Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In the example application's thankyou.jsp is this markup.

Code Block
XMLhtmlXML
html
1thankyou.jsp Struts if Tag

<s:if test="personBean.over21">

    <p>You are old enough to vote!</p>
    
</s:if>

<s:else>

   <p>You are NOT old enough to vote.</p>

</s:else>

...

The value of the test attribute must be an expression that evaluates to true or false, but doesn't need to be a method call that returns a boolean. For example this s:if tag that is in thankyou.jsp has a more complicated expression.

Code Block
XMLhtmlXML
html
1thankyou.jsp Struts if Tag

<s:if test="personBean.carModels.length > 1">

	<p>Car models

</s:if>

<s:else>

   <p>Car model


</s:else>


...

The Struts iterator tag is used to generate a loop that iterates over each item in a collection. In the thankyou.jsp is this markup.

Code Block
XMLhtmlXML
html
1thankyou.jsp Struts iterator Tag

<table style="margin-left:15px">
	
	<s:iterator value="personBean.carModels">
	
		<tr><td><s:property /></td></tr>

	</s:iterator>
	
	
</table>


The goal of this code is to create an HTML table with a row that display a car model selected by the user on the edit page. The car models the user selects on the edit page are stored in the carModels field (a String array) of the personBean object (of class Person).

...

If the collection contains objects that have multiple fields, then you should use the value attribute of the s:property tag to determine what field to display. For example:

Code Block
XMLhtmlXML
html
1thankyou.jsp Struts iterator Tag

<table style="margin-left:15px">

	<s:iterator value="states" >
	
		<tr><td><s:property value="stateAbbr" /></td> <td><s:property value="stateName" /></tr>

	</s:iterator>
	
</table>


...