Struts2 struts标签
Posted hoho_12
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2 struts标签相关的知识,希望对你有一定的参考价值。
1.index.jsp
<body> 访问属性 <a href="tags.action?username=u&password=p">tags</a> </body>
2.struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="struts.ui.theme" value="simple"/> <package name="tags" extends="struts-default"> <action name="tags" class="com.wxh.action.TagsAction"> <result>/tags.jsp</result> </action> </package> </struts>
3.tags.jsp
<body> <ol> <li>property:<s:property value="username"/></li> <li>property 取值为字符串:<s:property value="'username'"/></li> <li>property 设定默认值:<s:property value="admin" default="管理员"/></li> <li>property 设定为html:<s:property value="'<hr/>'" escape="true"/></li> <hr/> <li>set 设定adminName值(默认为request和ActionContext):<s:set var="adminName" value="username"/></li> <li>set 从request取值:<s:property value="#request.adminName"/></li> <li>set 从ActionContext取值:<s:property value="#adminName"/></li> <li>set 设定范围:<s:set name="adminPassword" value="password" scope="page"/></li> <li>从相应的范围取值:<%=pageContext.getAttribute("adminPassword")%></li> <li>set 设定var,范围为ActionContext:<s:set name="adminPassword" value="password" var="a"/></li> <li>使用#取值:<s:property value="#a"/></li> <hr/> <li>bean 定义bean,并使用param来设定新的属性值; <s:bean name="com.wxh.action.Dog"> <s:param name="name" value="'smalldog'"></s:param> </s:bean> <li>bean查看debug情况: <s:bean name="com.wxh.action.Dog" var="myDog"></s:bean> </li> <hr/> <li>include include1.html 包含静态英文文件 <s:include value="/include1.html"></s:include> </li> <li>include include2.html 包含静态中文文件 <s:include value="/include2.html"></s:include> </li> <li>include include1.html 包含静态英文文件,说明%用法 <s:set var="incPage" value="'/include1.html'"/> <s:include value="%{#incPage}"></s:include> </li> <hr/> <li> <s:fielderror fieldName="fielderror.test" theme="simple"></s:fielderror> </li> <hr/> <li>if elseif else: age=<s:property value="#parameters.age[0]"/><br/> <s:if test="#parameter.age[0]<0">wrong age!</s:if> <s:elseif test="#parameter.age[0]<20">too young!</s:elseif> <s:else>yeah!</s:else><br/> <s:if test="#parameters.aaa==null">null</s:if> </li> <hr/> <li>遍历集合:<br/> <s:iterator value="{1,2,3}"> <s:property/> </s:iterator> </li> <li>自定义变量:<br/> <s:iterator value="{'aaa','bbb','ccc'}" var="x"> <s:property value="#x.toUpperCase()"/> </s:iterator> </li> <li>使用status:<br/> <s:iterator value="{'aaa','bbb','ccc'}" status="status"> <s:property/>| 遍历过得元素总数:<s:property value="#status.count"/>| 遍历过得元素索引:<s:property value="#status.index"/>| 当前是偶数?:<s:property value="#status.even"/>| 当前是奇数?:<s:property value="#status.odd"/>| 是第一个元素吗?:<s:property value="#status.first"/>| 是最后一个元素吗?:<s:property value="#status.last"/> <br/> </s:iterator> </li> <li> <s:iterator value="#{1:'a',2:'b',3:'c'}" > <s:property value="key"/>|<s:property value="value"/><br/> </s:iterator> </li> <li> <s:iterator value="#{1:'a',2:'b',3:'c'}" var="x"> <s:property value="#x.key"/>|<s:property value="#x.value"/><br/> </s:iterator> </li> <s:debug></s:debug> </ol> </body>
4.TagsAction.java
package com.wxh.action; import com.opensymphony.xwork2.ActionSupport; public class TagsAction extends ActionSupport{ private String password; private String username; public TagsAction(){ } public String execute(){ this.addFieldError("fielderror.test","wrong!"); return SUCCESS; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
package com.wxh.action; public class Dog { public Dog(){ } public Dog(String anme){ super(); this.name=name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return "dog:"+name; } }
package com.wxh.action; public class S { public static String STR="STATIC STRING"; public static String s(){ return "static method"; } }
7.User.java
package com.wxh.action; public class User { private int age; public User(){ System.out.println("user.constructor!"); } public User(int age){ super(); this.age=age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return "user"+age; } }
8.include1.html
<body> include1.html </body>
9.include2.html
<body> 中文 </body>
以上是关于Struts2 struts标签的主要内容,如果未能解决你的问题,请参考以下文章