标签:
通用标签:property, set, bean,include,param,debug
控制标签: if elseif else, iterator, subset
UI标签:theme, 默认simple xhtml, css_xhtml, ajax
AJAX标签:补充
$#%的区别
$用于i18n, struts配置文件
#取得actionContext的值
%将原本文本属性解析为ognl,
index.jsp:
<?xml version="1.0" encoding="GB18030" ?> <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <% String contextPath = request.getContextPath(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>Insert title here</title> </head> <body> 访问属性 <a href="<%=contextPath %>/tags.action?username=u&password=p">tags</a> </body> </html>
tagsAction:
package com.bjsxt.struts2.tags; 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 String getUsername() { return username; } public void setPassword(String password) { this.password = password; } public void setUsername(String username) { this.username = username; } }
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!--<constant name="struts.enable.DynamicMethodInvocation" value="false" />--> <constant name="struts.devMode" value="true" /> <!--<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>--> <constant name="struts.ui.theme" value="simple" /> <package name="tags" extends="struts-default"> <action name="tags" class="com.bjsxt.struts2.tags.TagsAction"> <result>/tags.jsp</result> </action> </package> </struts>
tags.jsp:
<!--property标签-->
<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 />
<!--set标签--> 变量名称太长等换名时使用
<!--set有个scope, 可以选择page, request,session, application,action,默认是action和request-->
不写scope, 就说默认action和request, 如果指定其他scope, 就不能用action取值
<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 设定范围: page<s:set name="adminPassword" value="password" scope="page"/></li> <li>set 从相应范围取值: <%=pageContext.getAttribute("adminPassword") %></li>
<li>set 设定var,范围为session: <s:set var="adminPassword" value="password" scope="session"/></li> <li>set 使用#取值: <s:property value="#adminPassword"/> </li> <!--取不到--> <li>set 从相应范围取值: <s:property value="#session.adminPassword"/> </li> <hr />
<!--bean标签-->
<li>bean 定义bean,并使用param来设定新的属性值: <s:bean name="com.bjsxt.struts2.tags.Dog" > <s:param name="name" value="‘pp‘"></s:param> <s:property value="name"/> </s:bean> </li> <!--与上面方式的区别是下面的有个var,就会放入action中,即便</s:bean>结束, var已经存在于actioncontext里,也可以从actioncontext里取出来-->
<li>bean 查看debug情况: <s:bean name="com.bjsxt.struts2.tags.Dog" var="myDog"> <s:param name="name" value="‘oudy‘"></s:param> </s:bean> 拿出值: <s:property value="#myDog.name"/> </li> <hr />
<!--include标签--> 尽量不用
<%@ include> 静态包含
<!include> 动态包含
<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> <!--将#incPage强制转换成OGNL表达式-->
</li> <hr />
$,#,%区别:
$: 用于i18n和struts配置文件
#:取得actionContext的值
%:将原本文本属性解析为ognl, 对于本来就是ognl的不起作用
<!--fieldError标签-->
<li> <s:fielderror fieldName="fielderror.test" theme="simple"></s:fielderror> </li>
<!--ifelse标签-->
url里加入 &age=9&age=20
<li>if elseif else:
age = <s:property value="#parameters.age[0]" /> <br />
<s:set var="age" value="#parameters.age[0]" />
<s:if test="#age < 0">wrong age!</s:if>
<s:elseif test="#parameters.age[0] < 20">too young!</s:elseif>
<s:else>yeah!</s:else><br />
<s:if test="#parameters.aaa == null">null</s:if>
</li>
<hr />
标签:
原文地址:http://www.cnblogs.com/wujixing/p/5175732.html