Struts2通用标签
1.Set标签赋予变量一个特定范围内的值. 当希望给一个变量赋一个复杂的表达式,每次访问该变量而不是复杂的表达式时用到.其在两种情况下非常有用: 复杂的表达式很耗时 (性能提升) 或者很难理解 (代码可读性提高).
TagAction(Cal有 num1属性和num2属性):
public class TagAction implements ModelDriven<Cal>{
private Cal cal1=new Cal();
private Cal cal2;
private String num1;
private String result;
public String execute(){
System.out.println(cal1);//cal1.getNum1()
System.out.println(cal2);
System.out.println(num1);
return "demo3";
}
public Cal getCal2() {
return cal2;
}
public void setCal2(Cal cal2) {
this.cal2 = cal2;
}
public String getNum1() {
return num1;
}
public void setNum1(String num1) {
this.num1 = num1;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public Cal getModel() {
// TODO Auto-generated method stub
return cal1;
}
}
配置(struts2-sy.xml):
<action name="tagAction" class="com.three.web.TagAction">
<result name="demo3">/demo3.jsp</result>
demo1.jsp:
<h1>struts的通用标签</h1>
<a href="${pageContext.request.contextPath }/sy/tagAction.action?num1=20&cal2.num1=99&result=666">测试9</a>
demo3.jsp:
<h3>set</h3>
<!--在ognl上下文的根对象中取result值,赋给test1 -->
<s:set var="test1" value="result"></s:set> <!-- var 非根对象 -->
${test1 },${requestScope.test1 }
如果觉得有些麻烦,繁琐,就使用下面最为简洁的方式:
<ww:set name="personName" value="person.name"/>
Hello, <ww:property value="#personName"/>. How are you?
2.property:得到’value’的属性,如果value没提供,默认为堆栈顶端的元素.
<h3>property</h3>
<!--取栈顶,取cal1(model) -->
<s:property/>,
<!--取根对象最上面的对象的属性 -->
<s:property value="num1"/>,
<!--取根对象的cal2对象num1属性 -->
<s:property value="cal2.num1"/>,
<!--取非根对象的cal2对象的num1值 -->
<s:property value="#request.cal2.num1"/>
简洁方式:
<ww:push value="myBean">
<!-- Example 1: -->
<ww:property value="myBeanProperty" />
<!-- Example 2: -->
<ww:property value="myBeanProperty" default="a default value" />
</ww:push>
<!-- Example 1 显示出myBean's getMyBeanProperty() 的执行结果. -->
<!-- Example 2 显示出myBean's getMyBeanProperty() 的执行结果,如果是null,则显示缺省(default)值. -->
3.push值到堆栈中,方便应用
<h3>push</h3><!-- 弹栈,压栈 -->
<!-- push就是将你需要的值放到栈顶,便于页面获取 -->
<s:property/>
<s:push value="result">
<s:property/>
</s:push>
<s:property/>
简洁:
<ww:push value="user">
<ww:propery value="firstName" />
<ww:propery value="lastName" />
</ww:push>
将user的值push到栈中,从而使property标签的能够获取user的属性(firstName, lastName etc)
<ww:push value="myObject"> ----- (1)
<ww:bean name="jp.SomeBean" id="myBean"/> ----- (2)
<ww:param name="myParam" value="top"/> ----- (3)
</ww:bean>
</ww:push>
当在 (1) 时, myObject 在栈顶
当在 (2) 时, jp.SomeBean 在栈顶, 也在stack's context中以myBean为键值存在
当在 (3) 时, top得到jp.SomeBean的实例
<ww:push value="myObject"> ---(A)
<ww:bean name="jp.SomeBean" id="myBean"/> ---(B)
<ww:param name="myParam" value="top.mySomeOtherValue"/> ---(C)
</ww:bean>
</ww:push>
当在 (A) 时, myObject 在栈顶
当在 (B) 时, jp.SomeBean 在栈顶, 也在stack's context中以myBean为键值存在
当在 (C) 时, top指向jp.SomeBean的实例.所以top.mySomeOtherValue调用SomeBean的 mySomeOtherValue() 方法
<ww:push value="myObject"> ---- (i)
<ww:bean name="jp.SomeBean" id="myBean"/> ---- (ii)
<ww:param name="myParam" value="[1].top"/> -----(iii)
</ww:bean>
</ww:push>
当在 (i) 时, myObject 在栈顶
当在 (ii) 时, jp.SomeBean 在栈顶, 后面是myObject
当在 (iii) 时, [1].top返回myObjec
4.action:通过指定命名空间和action名称,该标签允许你在jsp页面直接调用Action. 标签体用来渲染Action执行结果. 除非你设定了executeResult参数为true,否则你在xwork.xml中为该Action指定的Result Processor不会执行.
<h3>action</h3>
<!--action通常用来请求后台,获取初始化数据的 -->
<s:action name="tagAction" namespace="/sy" var="test2"></s:action>
<s:property value="#test2.cal2"/>
简洁:
public class ActionTagAction extends ActionSupport {
public String execute() throws Exception {
return "done";
}
public String doDefault() throws Exception {
ServletActionContext.getRequest().setAttribute("stringByAction",
"This is a String put in by the action's doDefault()");
return "done";
}
}
<xwork>
....
<action name="actionTagAction1" class="tmjee.testing.ActionTagAction">
<result name="done">success.jsp</result>
</action>
<action name="actionTagAction2" class="tmjee.testing.ActionTagAction"
method="default">
<result name="done">success.jsp</result>
</action>
....
</xwork>
<div>The following action tag will execute result and include it in this page
</div>
<br />
<ww:action name="actionTagAction" executeResult="true" />
<br />
<div>The following action tag will do the same as above,
but invokes method specialMethod in action</div>
<br />
<ww:action name="actionTagAction!specialMethod" executeResult="true" />
<br />
<div>The following action tag will not execute result,
but put a String in request scope under an id "stringByAction"
which will be retrieved using property tag</div>
<ww:action name="actionTagAction!default" executeResult="false" />
<ww:property value="#attr.stringByAction" />
5.url:该标签用于创建url,可以通过"param"标签提供request参数.
注意:
当includeParams的值时’all’或者’get’, param标签中定义的参数将有优先权,也就是说其会覆盖其他同名参数的值.
<!--url标签是为了生成地址所用,注意与a标签区分 -->
<s:url namespace="/sy" action="tagAction" var="test3"></s:url>
<s:property value="test3"/>
<a href='<s:property value="test3"/>'>xxx</a>
<s:a href="%{#test3}">aaa</s:a>
简洁:
<-- Example 1 -->
<ww:url value="editGadget.action">
<ww:param name="id" value="%{selected}" />
</ww:url>
<-- Example 2 -->
<ww:url action="editGadget">
<ww:param name="id" value="%{selected}" />
</ww:url>
<-- Example 3-->
<ww:url includeParams="get" >
<:param name="id" value="%{'22'}" />
</ww:url>
6.param:为其他标签提供参数,比如include标签和bean标签.
参数的name属性是可选的,如果提供,会调用Component的方法addParameter(String, Object),如果不提供,则外层嵌套标签必须实现UnnamedParametric接口(如TextTag).
该标签的两个属性
name (String) - 参数名
value (Object) - 参数值
注意 : value的提供有两种方式,通过value属性或者标签中间的text,不同之处我们看一下例子:
<param name="color">blue</param> <-- (A) -->
<param name="color" value="blue"/> <-- (B) -->
(A)中,参数值会以String的格式放入statck. (B)中该值会以java.lang.Object的格式放入statck
<h3>param</h3>
<!--两种赋值方式: ognl表达式, 字符串 -->
<s:url namespace="/sy" action="tagAction" var="test4">
<s:param name="test5">aaa</s:param>
<s:param name="test6" value="num1"></s:param>
</s:url>
<s:property value="test4"/>
简洁:
<pre>
<ui:component>
<ui:param name="key" value="[0]"/>
<ui:param name="value" value="[1]"/>
<ui:param name="context" value="[2]"/>
</ui:component>
</pre>
这里key作为标识符,value在当前的OgnlValueStack上被当作一个OGNL的表达式来求值获得结果.(原文:where the key will be the identifier and the value the result of an OGNL expression run against the current OgnlValueStack.)
7.date:时间
date
<%
request.setAttribute("currentDate", new Date());
request.setAttribute("score", new Integer(70));
request.setAttribute("names", new String[]{"gay","ls","ww"});
<!-- 加3条数据,方便接下来的标签使用 -->
%>
<s:date name="#request.currentDate" format="yyyy-MM-dd"/>
<h3>debug</h3>
<s:debug/>
控制标签:
iterator/if/elseif/else:
(非根对象需要#在前面)
<ul>
<s:iterator var="v" value="#request.names">
<li>
<s:property value="#v"/>
</li>
</s:iterator>
</ul>
<s:if test="#request.score >80">
A
</s:if>
<s:elseif test="#request.score >60">
B
</s:elseif>
<s:else>C</s:else>