Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Info

The example code for this tutorial, Control_Tags_Struts2_Mvncontrol_tags, is available on Google Code - httpat https://code.googlegithub.com/papache/struts2-examples/downloads/listImage Removed. After downloading and unzipping the file, you'll have a folder named Control_Tags_Struts2_Mvn. In that folder will be a README.txt file with instructions on now to build and run the example application. struts-examples

Introduction

Struts 2 has several control tags that can be used in the view. This tutorial will discuss and show examples of how to use the Struts 2 if and iterator tags. For more information about these and other control tags visit tags reference.

...

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

Code Block
1thankyou.jsp Struts if Tag
languagexml
titlehtmlhtml1thankyou.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 Struts if tag has a test attribute. The value of the test attribute must evaluate to true or false. If true the statements between the opening and closing s:if tags will be executed. If false, the statements between the opening and closing s:else tags will be executed. Note that s:else tags come after the closing s:if tag and that the s:else tags are not required.

...

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.

html
Code Block
html
1thankyou.jsp Struts if Tag
languagexml


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

	<p>Car models

</s:if>

<s:else>

   <p>Car model


</s:else>


The purpose of the above markup is to use either "Car model" or "Car models" depending on how many car models the user selected on the edit page. So the value for the test attribute of this iterator tag gets the length of the carModels String array and compares that to 1. If it's greater then 1, the correct grammar is "Car models" else the correct grammar is "Car model".

...

Code Block
html
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
htmlhtml1thankyou.jsp Struts iterator Tag
languagexml


<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>


The value of the iterator tag is states, which causes the Struts 2 framework to call the getStates method of the Action class (EditAction.java). The getStates method returns a List of State objects. The State class has two fields stateAbbr and stateName, both having the appropriate get method. The iterator will loop over each State object stored in the collection. Each time through the loop, the Struts 2 framework will have a reference to the current State object and will call getStateAbbr and getStateName methods for that current State object.

...