Monday, January 2, 2012

ATG Nesting Servlet Beans



The HTML produced by open parameters can contain dynamic elements such as dsp:valueof tags. The
open parameters of one servlet bean can also include other servlet beans.
The following example specifies to display a list of students who live in a dormitory, and under each
student name list registered courses. It does this by nesting ForEach servlet bean: the outer servlet bean
displays the student names, and the inner servlet bean displays the courses.

<dsp:droplet name="/atg/dynamo/droplet/ForEach"> 
  <dsp:param bean="/dorms/Markley.students" name="array"/> 
    <dsp:oparam name="output"> 
    <p><dsp:valueof param="element.lastName"/>&nbsp;, 
       <dsp:valueof param="element.firstName"/>&nbsp;:</p> 
    <dsp:droplet name="/atg/dynamo/droplet/ForEach"> 
       <dsp:param param="element.subjects" name="array"/> 
         <dsp:oparam name="outputStart"> <ul> 
         <dsp:oparam name="output"> 
           <li><dsp:valueof param="element"/> </li> 
         </dsp:oparam> 
         <dsp:oparam name="outputEnd"> </ul> 
     </dsp:droplet> 
    </dsp:oparam> 
</dsp:droplet>

When this page is compiled, the following actions occur:

1. The outer ForEach sets its array parameter to Markley.students, an array of
Student components. The outer ForEach element points to the first Student
component in this array.

2. The outer ForEach bean displays the current value of element.name, the name
property of the current Student component.

3. The inner ForEach bean sets its array parameter to element.subjects. This
parameter points to the first subjects String in the current Student component.

4. The inner ForEach bean iterates over the elements of its array parameter, displaying
each String in element.subjects.
Note: The element parameter of a given servlet bean has scope only within that bean;
changes to its value have no effect on the element parameters of servlet beans that
are nested outside and inside the current bean.

5. After the inner ForEach processes all elements in element.subjects, it returns
control to the outer ForEach.

6. The outer ForEach element parameter points to the second element of the
Markley.students array.

7. Steps 3-5 repeat, this time using the next Student component.

8. This process continues for each component in the Markley.students array



The previous example generates HTML as follows:

<p>Williams, Susan:</p>
<ul>
<li>Linguistics</li>
<li>Biology</li>
<li>Latin</li>
<li>Philosophy</li>
</ul>
<p>Thomas, Frank:</p>
<ul>
<li>Physics
<li>American History
<li>Psychology
<li>French
</ul>

Referencing parameters in outer servlet beans
Servlet bean parameters have scope only within the bean itself. When nesting occurs, the parameters
within each bean are unaffected by corresponding parameters in the outer- and inner-nested beans.
However, an inner bean can access the values of an outer bean parameter by using to define another
parameter in the outer servlet bean, and setting it to the value of a parameter in the current bean that the
inner beans can access.
For example, you can modify the JSP shown earlier and obtain similar output, as follows:

<dsp:droplet name="/atg/dynamo/droplet/ForEach"> 
  <dsp:param bean="/dorms/Markley.students" name="array"/> 
    <dsp:oparam name="output"> 
    <dsp:setvalue param="outerElement" paramvalue="element"/> 
    <dsp:droplet name="/atg/dynamo/droplet/ForEach"> 
    <dsp:param param="element.subjects" name="array"/> 
       <dsp:oparam name="outputStart"> 
          <p><dsp:valueof param="outerElement.lastName"/>&nbsp;, 
             <dsp:valueof param="outerElement.firstName"/>&nbsp;:</p> 
          <ul> 
          <dsp:oparam name="output"> 
            <li><dsp:valueof param="element"/> </li> 
          </dsp:oparam> 
       <dsp:oparam name="outputEnd"> </ul> 
    </dsp:droplet> 
    </dsp:oparam> 
</dsp:droplet> 

No comments:

Popular Posts