Struts2 官方教程之Struts Tags(六)——Generic Tags(Control Tags )
来源:互联网 发布:java安装教程详细 编辑:程序博客网 时间:2024/04/29 10:49
开头要说的:在早期的应用开发中,表现层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.
- if
- elseif
- else
- append
- generator
- iterator
- merge
- sort
- subset
- a
- action
- bean
- date
- debug
- i18n
- include
- param
- property
- push
- set
- text
- url
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
有两个属性,但是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:
falseName
Required
Default
Evaluated
Type
Description
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 iteratorgenerated, namely aaa, bbb and ccc respectively because count attribute is set to 3Example 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 specifiedby 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. Withthis 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:
falseName
Required
Default
Evaluated
Type
Description
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>
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 Displayfirstelement 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:
falseName
Required
Default
Evaluated
Type
Description
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>
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:
falseName
Required
Default
Evaluated
Type
Description
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:
falseName
Required
Default
Evaluated
Type
Description
Indicate the number of entries to be in the resulting subset iterator
子集元素个数。
deciderfalse falseorg.apache.struts2.util.SubsetIteratorFilter.DeciderExtension to plug-in a decider to determine if that particular entry is to be included in the resulting subset iteratoridfalse falseStringDeprecated. Use 'var' insteadsourcefalse falseStringIndicate the source of which the resulting subset iterator is to be derived base onstartfalse falseIntegerIndicate 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由开发都自己指定是否选择选中该元素varfalse falseStringThe name to store the resultant iterator into page context, if such name is suppliedExamples
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>
- Struts2 官方教程之Struts Tags(六)——Generic Tags(Control Tags )
- Struts2 官方教程之Struts Tags(七)——Generic Tags(Data Tags )
- Struts2 官方教程之Struts Tags(九)——UI Tags(Form Tags)
- Struts2 官方教程之Struts Tags(八)——UI Tags(Themes and Templates)
- Struts2 官方教程之Using Struts 2 Tags (五)
- Struts2 官方教程:控制标签(Control Tags)
- Struts2教程三——Using Struts 2 Tags
- Struts 2 Control Tags
- struts2——Tags!!!
- Struts2——Tags
- struts2.1 Control Tags
- Struts2--struts-tags
- Struts2--struts-tags (转)
- Struts2---Struts-Tags
- Struts2官方教程:表单标签 Form Tags
- Struts 官方文档——使用Struts Tags
- tags
- struts2 标签初识 Control Tags
- eclipse常用快捷方式
- Android之ViewPager实现欢迎引导页面
- 容易忘记指针运算操作
- 单片式以太网控制芯片W7100A / W7100 Debugger指南(二)
- 变频器是如何节能的?
- Struts2 官方教程之Struts Tags(六)——Generic Tags(Control Tags )
- Ubuntu_ubuntu下统计目录及其子目录文件个数
- WinRAR 命令行各参数用法
- 驱动程序内存分配
- 串口通信
- 为什么虚函数必须是非静态成员函数 构造函数能为static吗?
- 堆排序heapsort
- 解压 tgz 文件
- win7下面vs2008试用版升级到正版步骤