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"/> , <dsp:valueof param="element.firstName"/> :</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:oparm name="outputEnd"> </ul> </dsp:droplet> </dsp:oparam> </dsp:droplet>
When this page is compiled, the following actions occur:
The outer ForEach sets its
array
parameter toMarkley.students
, an array of Student components. The outer ForEachelement
points to the first Student component in this array.The outer ForEach bean displays the current value of
element.name
, thename
property of the current Student component.The inner ForEach bean sets its
array
parameter toelement.subjects
. This parameter points to the firstsubjects
String in the current Student component.The inner ForEach bean iterates over the elements of its
array
parameter, displaying each String inelement.subjects
.Note: The
element
parameter of a given servlet bean has scope only within that bean; changes to its value have no effect on theelement
parameters of servlet beans that are nested outside and inside the current bean.After the inner ForEach processes all elements in
element.subjects
, it returns control to the outer ForEach.The outer ForEach
element
parameter points to the second element of theMarkley.students
array.Steps 3-5 repeat, this time using the next Student component.
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"/> , <dsp:valueof param="outerElement.firstName"/> :</p> <ul> <dsp:oparam name="output"> <li><dsp:valueof param="element"/> </li> </dsp:oparam> <dsp:oparm name="outputEnd"> </ul> </dsp:droplet> </dsp:oparam> </dsp:droplet>