May 15, 2021 Struts2
The Struts2 label has a set of labels that make it easy to control the processes that the page performs. H ere are the main Struts2 control labels:
These labels perform the basic conditional flow found in each language. T he "If" label can be used alone or with the "Else If" label and with a single or multiple "Else" label, as follows:
<s:if test="%{false}"> <div>Will Not Be Executed</div> </s:if> <s:elseif test="%{true}"> <div>Will Be Executed</div> </s:elseif> <s:else> <div>Will Not Be Executed</div> </s:else>
These itators iterate a value. I
terative values can be any of the following: java.util.Collection, java.util.Iterator.
When iterating through an itator, you can
use the Sort label
to sort the results, or you can use the
SubSet label
to get a subset of a list or array.
The following example retrieves the value of the getDays() method of the current object on the value stack and iterates with it.
The label prints the current value of the iterator.
<s:iterator value="days"> <p>day is: <s:property/></p> </s:iterator>
These merge tags take two or more lists as arguments and combine them as follows:
<s:merge var="myMergedIterator"> <s:param value="%{myList1}" /> <s:param value="%{myList2}" /> <s:param value="%{myList3}" /> </s:merge> <s:iterator value="%{#myMergedIterator}"> <s:property /> </s:iterator>
These append tags take two or more lists as parameters and attach them all together, as follows:
<s:append var="myAppendIterator"> <s:param value="%{myList1}" /> <s:param value="%{myList2}" /> <s:param value="%{myList3}" /> </s:append> <s:iterator value="%{#myAppendIterator}"> <s:property /> </s:iterator>
These generator tags generate iterators based on the val properties provided. T he following generator label generates an iterator and prints it out using the iterator label.
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}"> <s:iterator> <s:property /><br/> </s:iterator> </s:generator>