web.xml配置如下:
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
struts.xml配置如下:
<package name="tags" extends="struts-default"> <action name="tags" class="com.mengya.action.TagsAction"> <result name="success">/tags.jsp</result> </action> </package>
TagsAction如下:
public class TagsAction extends ActionSupport { private String username; private String password; @Override public String execute() throws Exception { this.addFieldError("actionError", "error!!"); 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; } }
Dog Bean如下:
public class Dog { private String dogName; public String getDogName() { return dogName; } public void setDogName(String dogName) { this.dogName = dogName; } @Override public String toString() { return "Dog:DogName=" + dogName; } }
页面struts标签使用如下:
<a href="tags.action?username=u&password=p&age=9">Struts2Tags</a> <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="false" /> </li> <hr /> <li> set 设定mengya的值(默认为Request和ActionContext): <s:set var="mengya" value="username"></s:set> </li> <li> set 从request中取值: <s:property value="#request.mengya" /> </li> <li> set 从ActionContext中取值: <s:property value="#mengya" /> </li> <li> set 设定范围: <s:set var="adminPassword" value="password" scope="session" /> </li> <li> set 从相应的范围取值: <%=session.getAttribute("adminPassword")%> </li> <li> set 从相应的范围取值: <s:property value="#session.adminPassword" /> </li> <hr> <li> bean 定义bean,并使用bean的param来设定的新属性值 <s:bean name="com.mengya.bean.Dog"></s:bean> <%-- 没有指定var则在</s:bean>之前会构造一个对象放在椎顶在</s:bean>结束之前则放到了actionContext中 --%> </li> <li> bean 定义bean,并使用bean的param来设定的新属性值 <s:bean name="com.mengya.bean.Dog" var="myDog"> <s:param name="dogName" value="'myDog'"></s:param> </s:bean> 取出值: <s:property value="#myDog" /> </li> <hr> <li> include 包含静态英文html <s:include value="/include1.html"></s:include> </li> <li> include 包含静态中文html <s:include value="/include2.html"></s:include> </li> <li> <font color="red">%的用法:%是将%{}中的内空强转成ognl表达示而不是普通字符串</font> <s:set var="incPage" value="'/include1.html'"></s:set> <br> <s:include value="incPage"></s:include> <br> <s:include value="%{incPage}"></s:include> <%-- 将incPage作为ognl表达式 --%> </li> <li> fielderror: <s:fielderror key="actionError" theme="simple"></s:fielderror> </li> <hr> <li> if elseif else: age = <s:property value="#parameters.age[0]" /> <s:if test="#parameters.age[0]<0"> age error! </s:if> <s:elseif test="#parameters.age[0]<20"> to young! </s:elseif> <s:else> OK! </s:else> | <s:set var="age" value="#parameters.age[0]"></s:set> <s:if test="age<0"> age error! </s:if> <s:elseif test="age<20"> to young! </s:elseif> <s:else> ok! </s:else> | <s:if test="#parameters.aaa==null">null</s:if> </li> <li> 遍历集合: <br> <s:iterator value="{'aa','bb','cc'}"> <s:property /> | </s:iterator> </li> <li> 自定义变量: <br> <s:iterator value="{'aa','bb','cc'}" var="temp"> <s:property value="#temp" /> | <s:property value="#temp.toUpperCase()" /> </s:iterator> </li> <li> 使用status: <br> <s:iterator value="{11,22,33}" 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> <s:debug></s:debug> </ol> 总结: $用于i18n和struts配置文件 #用于得到ActionContext的值 %将原本的文本属性解析为OGNL表达式,对于本来就是OGNL的属性不起做用 </body>