Nested Servlet Beans and Referencing Parameters

Because a servlet bean's parameters have scope only within the bean itself, when you nest one servlet bean inside another, the outer servlet bean is not affected by the parameter values in the inner servlet bean. However, it is possible for the inner bean to access the values of the outer bean's parameters by prepending two dots (..) to the parameter names to distinguish them from the inner bean's parameters of the same name.


Suppose, in the previous example, you wanted to display both the product's name and corresponding skus from the inner ForEach. You could do something like this:

<droplet bean="/atg/dynamo/droplet/ForEach">
  <param name="array" VALUE="/myBean.products">
    <oparam name="output">   
       <droplet bean="/atg/dynamo/droplet/ForEach">
       <param name="array" VALUE="param:element.skus">
       <oparam name="output">
        <p> <valueof param="..element.name"></valueof>   
         has the  SKUs
         <valueof param ="element"></valueof>.    
       </oparam>
       </droplet>
    </oparam>
</droplet>

In the first VALUEOF tag, ..element refers to the element parameter of the outer ForEach. In the second VALUEOF tag, element refers to the element parameter of the inner ForEach. The resulting HTML is:

<p>Samsung Galaxy  has the SKUS Samsung Galaxy B7510 .
<p>Samsung Galaxy  has the SKUS Samsung Galaxy pop s5570
<p>Samsung Galaxy  has the SKUS Samsung Galaxy pop i559


<p>Nokia has the SKUS Nokia Lumia 800 
<p>Nokia has the SKUS Nokia X2 
<p>Nokia has the SKUS Nokia C5 

You can use this dot notation with multiple levels of nesting. 
Parameters preceded by two dots refer to the servlet bean one level up; parameters preceded by three dots refer to the servlet bean two levels up; and so on. You can also use a single dot to refer to a parameter at the current level, but this is not necessary.

Another approach is to use the setvalue tag to define another parameter in the outer servlet bean, and set it to the value of the parameter you want to refer to from within the inner servlet bean. The following example uses this approach, and produces that same results as the example above.See the screen shot below.


Referencing parameter in nested servlet bean





0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...