这一篇中所用到的例子,似乎是李刚书上的,我没有看,是从老师的电脑上拷过来的;十分能说明问题;
具体的标签使用起来不是很难,忘记可以参考例子,去struts2文档查看更好,文档写的比较清楚;
话不多说,上代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'tags.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <ol> <li>action: <s:action var="t" name="tags01" executeResult="false" ignoreContextParams="false"> <s:param name="username" value="'javaxzbxzb'"></s:param> </s:action> <s:property value="#t.username"/> </li><hr/> <li>property:<s:property value="#t.username"/></li> <li>property取值为字符串:<s:property value="'xzb'"/></li> <li>property设定默认值:<s:property value="username" default="xzb"/></li> <li>property设定html:<s:property value="'<hr/>'" escape="true"/></li> <li>bean:定义bean,并使用param来设定新的属性值 <s:bean var="mycat" name="struts2.tags.Cat"> <s:param name="name" value="'blackCat'"/> <!--<s:property value="name"/>--> </s:bean> <s:property value="#mycat.name"/> </li><hr/> <li>set:设定值(默认是action):<s:set var="admin" value="#t.username"></s:set> <s:property value="admin"/> </li> <li>set从request中取值:<s:property value="#request.admin"/></li> <li>set从Stack Context中取值:<s:property value="#admin"/></li> <li>set的var属性;scope设为page<s:set var="test" value="'var test'" scope="page"></s:set> </li> <li>set:从page中取var属性值:<s:property value="#attr.test"/></li> <hr/> <li>push:<s:push value="#mycat"><s:property value="name"/></s:push></li> <li>include<br> include-JSP:<s:include value="included.jsp"> <s:param name="author" value="'薛增博'"></s:param> </s:include> include-HTML:<s:set var="in" value="'included.html'"></s:set> <s:include value="%{in}"></s:include> </li> <hr/> <li>if elseif else: <s:set var="XZB" value="'23'"></s:set> <s:property value="XZB" /> <br /> <s:if test="XZB<0">wrong</s:if> <s:elseif test="XZB<30">young man</s:elseif> <s:else>old man</s:else> <hr/> <li>iterator---list: <s:iterator value="{1,2,3,4,5,6,7,8,9}" begin="1" end="8" step="2"> <s:property/> </s:iterator><br/> iterator---map:<br/> <s:iterator value="#{1:'java1',2:'java2',3:'java3'}" status="s"> <s:property/> <s:property value="key"/><s:property value="value"/> <s:property value="#s.count"/> <s:property value="#s.index"/> <s:property value="#s.first"/> <s:property value="#s.last"/> <s:property value="#s.odd"/> <s:property value="#s.even"/><br/> </s:iterator> </li><hr/> <form action="tags02" method="post"> 年龄1:<input type="checkbox" value="12" name="myList1" /> 年龄2:<input type="checkbox" value="82" name="myList1" /> <input type="submit" value="submit" /> </form> </li><hr/> 集合标签:<br/> <li>append---jsp:<br/> <s:append var="appendList"> <s:param value="{'banana', 'apple', 'orange', 'cherry'}" /> <s:param value="{'香蕉', '苹果', '桔子', '樱桃'}" /> </s:append> <s:iterator value="#appendList"> <s:property/> </s:iterator><br/> append---action:<br/> <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> </li><hr/> <li>merge---jsp:<br/> <s:merge var="mergeList"> <s:param value="{'banana', 'apple', 'orange', 'cherry'}" /> <s:param value="{'香蕉', '苹果', '桔子', '樱桃'}" /> </s:merge> <s:iterator value="#mergeList"> <s:property/> </s:iterator></li><br/> merge---action: <s:merge var="myMergeIterator"> <s:param value="%{myList1}" /> <s:param value="%{myList2}" /> <s:param value="%{myList3}" /> </s:merge> <s:iterator value="#myMergeIterator"> <s:property /> </s:iterator><hr/> <li>generator:<br/> <s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" separator="," count="3" > <s:iterator> <s:property /> </s:iterator> </s:generator> </li> <li>sort: <s:bean var="myComparator" name="struts2.tags.MyComparator"/> <s:sort source="{'轻量级Java EE企业应用实战','疯狂Ajax讲义','Struts 2权威指南','疯狂XML讲义'}" comparator="#myComparator" var="sortedList"/> <s:iterator status="st" value="#attr.sortedList"> <s:property/> </s:iterator> </li> <li>subset: <s:bean var="myDecider" name="struts2.tags.MyDecider"></s:bean> <s:subset source="{'疯狂Java讲义','Struts 2权威指南','轻量级Java EE企业应用实战','疯狂Ajax讲义','疯狂XML讲义'}" decider="#myDecider"> <s:iterator> <s:property /> </s:iterator> </s:subset> </li> </ol> <s:debug></s:debug> </body> </html>
结合,strust2的文档和实例就可以明白何时用那个标签了,常用的便是这几个标签。
上面还有几个Action的代码一同贴出来:
package struts2.tags; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class TagsAction extends ActionSupport { private String username; private List<String> myList1; private List<String> myList2; private List<String> myList3; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public List<String> getMyList1() { return myList1; } public List<String> getMyList2() { return myList2; } public List<String> getMyList3() { return myList3; } public String execute() { myList1 = new ArrayList<String>(); myList1.add("1"); myList1.add("2"); myList1.add("3"); myList2 = new ArrayList<String>(); myList2.add("a"); myList2.add("b"); myList2.add("c"); myList3 = new ArrayList<String>(); myList3.add("A"); myList3.add("B"); myList3.add("C"); return "success"; } }
实体类:
package struts2.tags; public class Cat { private String name; public Cat(){ } public Cat(String name){ this.name=name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return "name:"+name; } }
两个工具类:
package struts2.tags; import org.apache.struts2.util.SubsetIteratorFilter; public class MyDecider implements SubsetIteratorFilter.Decider { public boolean decide(Object element) throws Exception { String str = (String)element; return str.indexOf("Java") > 0; } }
package struts2.tags; import java.util.Comparator; public class MyComparator implements Comparator { public int compare(Object element1, Object element2) { return element1.toString().length() - element2.toString().length(); } }
好了,作为参考,结束了。