开头要说的:在早期的应用开发中,表现层Jsp页面主要使用Jsp脚本来控制输出。这样,在Jsp中嵌套了java脚本,这种方式不管是可读性还是可维护性都很差,几乎使Jsp成为Java的子集。从Jsp1.1之后,才增加了Jsp标签库,这种情况才有所好转。
Generic tags are used for controlling the execution flow when the Jsppages render. These tags also allow for data extraction from places other than your action or the value stack, such asLocalization, JavaBeans, and including aditional URLs or action executions.
Generic tags用于控制显示,这些标签也可以从一些地方提取数据,但与action或者值栈不同,比如从Localization, JavaBeans以及url传递过来的值。
- Control Tags provide control flow, such as if, else, and iterator.
- Data Tags allow for data manipulation or creation, such as bean, push, and i18n.
Control Tags | Data Tags |
---|
Examples
<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>
Component for AppendIteratorTag, which jobs is to append iterators to form an appended iterator whereby entries goes from one iterator to another after each respective iterator is exhausted of entries.
用于将多个集合对象拼接起来,组成一个新的集合,通过这种拼接,从而允许通过<s:iterator../>就完成对多个集合的迭代。
Name | Required | Default | Evaluated | Type | Description |
---|---|---|---|---|---|
id | false | false | String | Deprecated. Use 'var' instead | |
var | false | false | String | The name of which if supplied will have the resultant appended iterator stored under in the stack's context |
有两个属性,但是var替代了id, 不赞成使用id属性 ,<s:append >可以包含多个<s:param>,每个<s:param>指定一个集合。
public class AppendIteratorTagAction extends ActionSupport {
private List myList1;
private List myList2;
private List myList3;
public String execute() throws Exception {
myList1 = new ArrayList();
myList1.add("1");
myList1.add("2");
myList1.add("3");
myList2 = new ArrayList();
myList2.add("a");
myList2.add("b");
myList2.add("c");
myList3 = new ArrayList();
myList3.add("A");
myList3.add("B");
myList3.add("C");
return "done";
}
public List getMyList1() { return myList1; }
public List getMyList2() { return myList2; }
public List getMyList3() { return myList3; }
<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>
Description
NOTE: JSP-TAG
Generate an iterator based on the val attribute supplied.
NOTE: The generated iterator will ALWAYS be pushed into the top of the stack, and poped at the end of the tag.
Parameters
Dynamic Attributes Allowed:false | |||||
Name | Required | Default | Evaluated | Type | Description |
---|---|---|---|---|---|
converter | false | false | org.apache.struts2.util.IteratorGenerator.Converter | The converter to convert the String entry parsed fromval into an object将字符串转化成对象 | |
count | false | false | Integer | The max number entries to be in the iterator集合中元素的总数 | |
id | false | false | String | Deprecated. Use 'var' instead | |
separator | true | false | String | The separator to be used in separating the val into entries of the iterator指定用于解析字符串的分隔符 | |
val | true | false | String | The source to be parsed into an iterator指定被解析的字符串 | |
var | false | false | String | The name to store the resultant iterator into page context, if such name is supplied如何使用该属性,将把生成的iterator对象放入stackContext中 |
Examples
Example One:
<pre>
Generate a simple iterator
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
</pre>
This generates an iterator and print it out using the iterator tag.
Example Two:
<pre>
Generate an iterator with count attribute
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="3">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
</pre>
This generates an iterator, but only 3 entries will be available in the iterator
generated, namely aaa, bbb and ccc respectively because count attribute is set to 3
Example Three:
<pre>
Generate an iterator with var attribute
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="4" separator="," var="myAtt" />
<%
Iterator i = (Iterator) pageContext.getAttribute("myAtt");
while(i.hasNext()) {
String s = (String) i.next(); %>
<%=s%> <br/>
<% }
%>
</pre>
This generates an iterator and put it in the PageContext under the key as specified
by the var attribute.
Example Four:
<pre>
Generate an iterator with comparator attribute
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" converter="%{myConverter}">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
public class GeneratorTagAction extends ActionSupport {
....
public Converter getMyConverter() {
return new Converter() {
public Object convert(String value) throws Exception {
return "converter-"+value;
}
};
}
...
}
</pre>
This will generate an iterator with each entries decided by the converter supplied. With
this converter, it simply add "converter-" to each entries.
Description
Iterator will iterate over a value. An iterable value can be any of: java.util.Collection, java.util.Iterator,
Parameters
Dynamic Attributes Allowed:false | |||||
Name | Required | Default | Evaluated | Type | Description |
---|---|---|---|---|---|
begin | false | 0 | false | Integer | if specified the iteration will start on that index |
end | false | Size of the 'values' List or array, or 0 if 'step' is negative | false | Integer | if specified the iteration will end on that index(inclusive) |
id | false | false | String | Deprecated. Use 'var' instead | |
status | false | false | false | Boolean | If specified, an instanceof IteratorStatus will be pushed into stack upon each iteration |
step | false | 1 | false | Integer | if specified the iteration index will be increased by this value on each iteration. It can be a negative value, in which case 'begin' must be greater than 'end' |
value | false | false | String | the iteratable source to iterate over, else an the object itself will be put into a newly created List | |
var | false | false | String | Name used to reference the value pushed into the Value Stack |
Examples
The following example retrieves the value of the getDays() method of the current object on the value stack and uses it to iterate over.The <s:property/> tag prints out the current value of the iterator.
<s:iterator value="days">
<p>day is: <s:property/></p>
</s:iterator>
The following example uses a Bean tag and places it into the ActionContext. The iterator tag will retrieve that object from the ActionContext and then calls its getDays() method as above. The status attribute is also used to create an IteratorStatus object, which in this example, its odd() method is used to alternate row colours:
<s:bean name="org.apache.struts2.example.IteratorExample" var="it">
<s:param name="day" value="'foo'"/>
<s:param name="day" value="'bar'"/>
</s:bean>
<p/>
<table border="0" cellspacing="0" cellpadding="1">
<tr>
<th>Days of the week</th>
</tr>
<p/>
<s:iterator value="#it.days" status="rowstatus">
<tr>
<s:if test="#rowstatus.odd == true">
<td style="background: grey"><s:property/></td>
</s:if>
<s:else>
<td><s:property/></td>
</s:else>
</tr>
</s:iterator>
</table>
The next example will further demonstrate the use of the status attribute, using a DAO obtained from the action class through OGNL, iterating over groups and their users (in a security context). The last() method indicates if the current object is the last available in the iteration, and if not, we need to separate the users using a comma:
<s:iterator value="groupDao.groups" status="groupStatus">
<tr class="<s:if test="#groupStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td><s:property value="name" /></td>
<td><s:property value="description" /></td>
<td>
<s:iterator value="users" status="userStatus">
<s:property value="fullName" /><s:if test="!#userStatus.last">,</s:if>
</s:iterator>
</td>
</tr>
</s:iterator>
The next example iterates over a an action collection and passes every iterator value to another action. The trick here lies in the use of the '[0]' operator. It takes the current iterator value and passes it on to the edit action. Using the '[0]' operator has the same effect as using <s:property />. (The latter, however, does not work from inside the param tag).
<s:action name="entries" var="entries"/>
<s:iterator value="#entries.entries" >
<s:property value="name" />
<s:property />
<s:push value="...">
<s:action name="edit" var="edit" >
<s:param name="entry" value="[0]" />
</s:action>
</push>
</s:iterator>
A loop that iterates 5 times
<s:iterator var="counter" begin="1" end="5" >
<!-- current iteration value (1, ... 5) -->
<s:property value="top" />
</s:iterator>
Another way to create a simple loop, similar to JSTL's <c:forEach begin="..." end="..." ...> is to use some OGNL magic, which provides some under-the-covers magic to make 0-n loops trivial. This example also loops five times.
<s:iterator status="stat" value="(5).{ #this }" >
<s:property value="#stat.count" /> <!-- Note that "count" is 1-based, "index" is 0-based. -->
</s:iterator>
A loop that iterates over a partial list
<s:iterator value="{1,2,3,4,5}" begin="2" end="4" >
<!-- current iteration value (2,3,4) -->
<s:property value="top" />
</s:iterator>
![]() | Please make sure you have read the Tag Syntax document and understand how tag attribute syntax works. |
Description
Component for MergeIteratorTag, which job is to merge iterators and successive call to the merged iterator will cause each merge iterator to have a chance to expose its element, subsequently next call will allow the next iterator to expose its element. Once the last iterator is done exposing its element, the first iterator is allowed to do so again (unless it is exhausted of entries).
Internally the task are delegated to MergeIteratorFilter
Example if there are 3 lists being merged, each list have 3 entries, the following will be the logic.
merge写append很相似,区别在于集合的顺序不同。左边是merge右边是append,相信一目了然
- Display first element of the first list Displayfirst element of the first list
- Display first element of the second list Display first element of the first list
- Display first element of the third list Display first element of the first list
- Display second element of the first list
- Display second element of the second list
- Display second element of the third list
- Display third element of the first list
- Display thrid element of the second list
- Display third element of the thrid list
Parameters
Dynamic Attributes Allowed:false | |||||
Name | Required | Default | Evaluated | Type | Description |
---|---|---|---|---|---|
id | false | false | String | Deprecated. Use 'var' instead | |
var | false | false | String | The name where the resultant merged iterator will be stored in the stack's context |
Examples
public class MergeIteratorTagAction extends ActionSupport {
private List myList1;
private List myList2;
private List myList3;
public List getMyList1() {
return myList1;
}
public List getMyList2() {
return myList2;
}
public List getMyList3() {
return myList3;
}
public String execute() throws Exception {
myList1 = new ArrayList();
myList1.add("1");
myList1.add("2");
myList1.add("3");
myList2 = new ArrayList();
myList2.add("a");
myList2.add("b");
myList2.add("c");
myList3 = new ArrayList();
myList3.add("A");
myList3.add("B");
myList3.add("C");
return "done";
}
}
<s:merge var="myMergedIterator1">
<s:param value="%{myList1}" />
<s:param value="%{myList2}" />
<s:param value="%{myList3}" />
</s:merge>
<s:iterator value="%{#myMergedIterator1}">
<s:property />
</s:iterator>
![]() | Please make sure you have read the Tag Syntax document and understand how tag attribute syntax works. |
Description
NOTE: JSP-TAG
A Tag that sorts a List using a Comparator both passed in as the tag attribute. If 'var' attribute is specified, the sorted list will be placed into the PageContext attribute using the key specified by 'var'. The sorted list will ALWAYS be pushed into the stack and poped at the end of this tag.
该标签用于对指定集合元素进行排序,必须提供自己的排序规则,要实现自己的Comparator,需要实现java.util.Comparator接口
Parameters
Dynamic Attributes Allowed:false | |||||
Name | Required | Default | Evaluated | Type | Description |
---|---|---|---|---|---|
comparator | true | false | java.util.Comparator | The comparator to use | |
id | false | false | String | Deprecated. Use 'var' instead | |
source | false | false | String | The iterable source to sort指定被排序的集合,不指定,则对ValueStack栈顶的集合进行排序。 | |
var | false | false | String | The name to store the resultant iterator into page context, if such name is supplied 指定该属性,则将生成的Iterator对象设置成page范围的属性。 |
Examples
USAGE 1:
<s:sort comparator="myComparator" source="myList">
<s:iterator>
<!-- do something with each sorted elements -->
<s:property value="..." />
</s:iterator>
</s:sort>
USAGE 2:
<s:sort var="mySortedList" comparator="myComparator" source="myList" />
<%
Iterator sortedIterator = (Iterator) pageContext.getAttribute("mySortedList");
for (Iterator i = sortedIterator; i.hasNext(); ) {
// do something with each of the sorted elements
}
%>
Description
NOTE: JSP-TAG
A tag that takes an iterator and outputs a subset of it. It delegates to org.apache.struts2.util.SubsetIteratorFilter internally to perform the subset functionality.
用于取得集合的子集。底层通过org.apache.struts2.util.SubsetIteratorFilter来实现
Parameters
Dynamic Attributes Allowed:false | |||||
Name | Required | Default | Evaluated | Type | Description |
---|---|---|---|---|---|
count | false | false | Integer | Indicate the number of entries to be in the resulting subset iterator 子集元素个数。 | |
decider | false | false | org.apache.struts2.util.SubsetIteratorFilter.Decider | Extension to plug-in a decider to determine if that particular entry is to be included in the resulting subset iterator | |
id | false | false | String | Deprecated. Use 'var' instead | |
source | false | false | String | Indicate the source of which the resulting subset iterator is to be derived base on | |
start | false | false | Integer | Indicate the starting index (eg. first entry is 0) of entries in the source to be available as the first entry in the resulting subset iterator由开发都自己指定是否选择选中该元素 | |
var | false | false | String | The name to store the resultant iterator into page context, if such name is supplied |
Examples
public class MySubsetTagAction extends ActionSupport {
public String execute() throws Exception {
l = new ArrayList();
l.add(new Integer(1));
l.add(new Integer(2));
l.add(new Integer(3));
l.add(new Integer(4));
l.add(new Integer(5));
return "done";
}
public Integer[] getMyArray() {
return a;
}
public List getMyList() {
return l;
}
public Decider getMyDecider() {
return new Decider() {
public boolean decide(Object element) throws Exception {
int i = ((Integer)element).intValue();
return (((i % 2) == 0)?true:false);
}
};
}
}
<!-- s: List basic -->
<s:subset source="myList">
<s:iterator>
<s:property />
</s:iterator>
</s:subset>
<!-- B: List with count -->
<s:subset source="myList" count="3">
<s:iterator>
<s:property />
</s:iterator>
</s:subset>
<!-- C: List with start -->
<s:subset source="myList" count="13" start="3">
<s:iterator>
<s:property />
</s:iterator>
</s:subset>
<!-- D: List with var -->
<s:subset var="mySubset" source="myList" count="13" start="3" />
<%
Iterator i = (Iterator) pageContext.getAttribute("mySubset");
while(i.hasNext()) {
%>
<%=i.next() %>
<% } %>
<!-- D: List with Decider -->
<s:subset source="myList" decider="myDecider">
<s:iterator>
<s:property />
</s:iterator>
</s:subset>