(1)struts.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="pkgOgnl" namespace="/pkgOgnl" extends="struts-default"> <action name="ActionValueStack_*" class="cn.cvu.action.ActionValueStack" method="{1}"> <result name="success">/success.jsp</result> </action> </package> </struts>(2)index.jsp:
<body> <a href="${pageContext.request.contextPath}/pkgOgnl/ActionValueStack_test.action">OGNL值栈测试</a> </body>
(1)Action类ActionValueStack:
package cn.cvu.action; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import cn.cvu.bean.Person; import cn.cvu.bean.User; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ValueStack; public class ActionValueStack extends ActionSupport { public String test(){ //作用域:requestScope-本次请求 HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("userName", "request_Eminem"); request.setAttribute("passWord", "rapking"); HttpServletResponse response = ServletActionContext.getResponse(); //作用域:sessionScope-本次会话 Map<String, Object> session = ServletActionContext.getContext().getSession(); session.put("userName", "session_Alizee"); session.put("passWord", "lover"); //作用域:applicationScope-整个应用程序 ServletContext servletContext = ServletActionContext.getServletContext(); servletContext.setAttribute("userName", "servletContext_Tupic"); servletContext.setAttribute("passWord", "raper"); //ValueStack作用域为request ValueStack valueStack = (ValueStack) request.getAttribute("struts.valueStack");//这个字符串是固定的 valueStack.getRoot().add(new Person()); //先被add的,先被取到 valueStack.getRoot().add(new User()); valueStack.set("userName", "valueStack_Micheal"); valueStack.set("passWord", "popking"); return SUCCESS; } }
(2)success.jsp:
<body> 0. EL表达式取值:<br> 使用“\${ requestScope.userName }” 获取userName:${ requestScope.userName };<br> 使用“\${ sessionScope.userName }” 获取userName:${ sessionScope.userName };<br> 使用“\${ applicationScope.userName }” 获取userName:${ applicationScope.userName };<br> <br> <hr> <br> 1. OGNL表达式:<b>#</b>用法:<br/> 1)使用<b>#</b>访问<b>非</b>根目录下的属性<br> 使用“< s:text name="#request.userName"/ >” 获取userName:<s:text name="#request.userName"></s:text><br> 使用“< s:text name="#session.userName"/ >” 获取userName:<s:text name="#session.userName"></s:text><br> 使用“< s:text name="#session['userName']"/ >” 获取userName:<s:text name="#session['userName']"></s:text><br> 使用“< s:text name="#attr.userName"/ >” 获取userName:<s:text name="#attr.userName"></s:text><br> <br> 2)直接访问根目录下的属性,此时<b>#</b>须省略<br> 使用“< s:text name="userName"/ >” 获取userName:<s:text name="userName"></s:text><br> 使用“< s:text name="passWord"/ >” 获取passWord:<s:text name="passWord"></s:text><br> <br> 3)创建Map:<br/> <s:radio list="#{'male':'男','female':'女'}" name="sex" label="性别"/><br> <br> <hr/> <br/> </body>
(3)测试:
(1)JavaBean:
public class Person { private String name = "person_name"; private Integer pwd = 123; private Integer age = 123; 。。。 } public class User { private String name = "user_name"; private Integer age = 26; 。。。 }
(2)Action类:
public class ActionValueStack extends ActionSupport { private String name = "valueStack_name"; //直接保存在root中,总是被先于其它同名变量取到 private Integer pwd = 990; public String test(){ //作用域:requestScope-本次请求 HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("userName", "request_Eminem"); request.setAttribute("passWord", "rapking"); HttpServletResponse response = ServletActionContext.getResponse(); //作用域:sessionScope-本次会话 Map<String, Object> session = ServletActionContext.getContext().getSession(); session.put("userName", "session_Alizee"); session.put("passWord", "lover"); //作用域:applicationScope-整个应用程序 ServletContext servletContext = ServletActionContext.getServletContext(); servletContext.setAttribute("userName", "servletContext_Tupic"); servletContext.setAttribute("passWord", "raper"); //ValueStack作用域为request ValueStack valueStack = (ValueStack) request.getAttribute("struts.valueStack");//这个字符串是固定的 //ValueStack valueStack = ServletActionContext.getContext().getValueStack(); valueStack.getRoot().add(new Person()); //先被add的,先被取到 valueStack.getRoot().add(new User()); valueStack.set("userName", "valueStack_Micheal"); valueStack.set("passWord", "popking"); return SUCCESS; } public String getName() {。。。} public void setName(String name) {。。。} public Integer getPwd() {。。。} public void setPwd(Integer pwd) {。。。} }
(3)success.jsp:
2. 深入理解值栈-ObjectStack:先取root内的变量,然后根据写入顺序读取:<br> <!-- valueStack_name --> 使用“< s:text name="name"/ >” 获取name:<s:text name="name"></s:text><br> <!-- 990 --> 使用“< s:text name="pwd"/ >” 获取pwd:<s:text name="pwd"></s:text><br> <!-- 123 --> 使用“< s:text name="age"/ >” 获取age:<s:text name="age"></s:text><br> <br> <hr/> <br/>
(4)测试:
(1)success.jsp:
3. OGNL表达式:百分号<b>%</b>标识字符串 的用法:<br> 使用百分号+大括号囊括,明确表示OGNL表达式:“< s:text name="%{#request.userName}"/ >” ,<br> 得到:<s:text name="%{#request.userName}"></s:text><br> <!-- request_Eminem --> 使用单引号囊括,表示普通字符串:“< s:text name="%{'request.userName'}" / >” ,<br> 得到:<s:text name="%{'request.userName'}"></s:text><br> <!-- request.userName --> <br> <hr/> <br/>
(2)测试:
使用一:在网址配置中传参:
(1)struts.xml配置参数:
<struts> <package name="pkgOgnl" namespace="/pkgOgnl" extends="struts-default"> <action name="ActionValueStack_*" class="cn.cvu.action.ActionValueStack" method="{1}"> <!-- 问号?之后为参数名,等号=后面为值栈中的属性 --> <result name="success">/success.jsp?paratemter=${userName}</result> <result name="input">/index.jsp</result> </action> </package> </struts>
(2)success.jsp:
4. OGNL表达式:美元符<b>$</b>用法:<br/> 1)读取在struts.xml中使用\${}在url中配置的属性:<br/> <s:text name="#parameters.paratemter[0]"></s:text><br/> <br/>
(3)测试:
使用二:在国际化中配置:
(1)国际化文件:
(2)struts.xml:
(3)success.jsp:
4. OGNL表达式:美元符<b>$</b>用法:<br/> 2)在国际化中使用\${}配置动态信息提示<br/> <s:text name="item.ognl"></s:text>
(4)测试:
(1)success.jsp:
5.<b>property</b>标签:<br/> 1)value,使用“< s:property value="userName"/ >” 获取值栈根中的userName:<s:property value="userName"/> <br/> 2)value,使用“< s:property value="#request.userName"/ >” 获取非根值栈中的userName:<s:property value="#request.userName"/> <br/> 3)default,使用“< s:property value="request.userName" default="默认值" / >” 获取值栈中不存在的userName:<s:property value="request.userName" default="默认值"/> <br/><br/> 4)escape,使用“< s:property value="%{'测试<hr>的使用'}" escape="true" / >”: <s:property value="%{'测试<hr>的使用'}" escape="true" /> 。默认 escape="true" <br/> 5)escape,使用“< s:property value="%{'测试<hr>的使用'}" escape="false" / >”: <s:property value="%{'测试<hr>的使用'}" escape="false" /> <br/>
(2)测试:
(1)success.jsp:
6.<b>set</b>标签:<br/> 1)使用“< s:set value="#request.userName" var="setName" />将value获取的值放入用var创建的新变量,<br/> <s:set value="#request.userName" var="setName" /> 然后使用< s:text name="setName"/ >取出:<s:text name="setName"/>, 可以看出默认保存在值栈的根目录。<br/><br/> 2)使用“< s:set value="#request.userName" var="setName" scope="session" /> 将value获取的值放入指定作用域,<br/> <s:set value="#request.userName" var="setName" scope="session"/> 然后使用< s:text name="#session.setName"/ >取出:<s:text name="#session.setName"/> 。
(2)测试:
(1)success.jsp:
7.<b>push</b>标签:<br/> 1)使用< s:push value="userName" >将一个已知的值放入值栈顶端作为临时变量:<br/> < s:push value="userName" > <br/> < s:property/ > 此时取到值栈顶端的变量 <s:push value="userName"> <s:property/> </s:push> <br/> < /s:push > <br/> 当push标签结束,栈顶的这个临时变量就销毁了。 <br/> 2)使用< s:push value="#session.userName" >将一个session中的值放入值栈顶端作为临时变量:<br/> < s:push value="#session.userName" > <br/> < s:property/ > 此时取到值栈顶端的变量 <s:push value="#session.userName"> <s:property/> </s:push> <br/> < /s:push > <br/> 当push标签结束,栈顶的这个临时变量就销毁了。 <br/>
(2)测试:
(1)JavaBean:
(2)Action:
public class ActionValueStack extends ActionSupport { public String test(){ List<Person> listPerson = new ArrayList<Person>(); for (int i = 0; i < 5; i++) { Person person = new Person("person"+i, 99920+i, 20+i); listPerson.add(person); } ValueStack valueStack = (ValueStack) request.getAttribute("struts.valueStack"); valueStack.set("persons", listPerson); return SUCCESS; } }
(3)success.jsp:
8.<b>iterator</b>标签:<br/> <s:iterator value="persons" var="person"> <!-- 遍历过程中,person会被放入root和ognl上下文 --> <!-- 此时须#开头访问变量 --> <s:property value="#person"/><br/> <!-- 此时用.调用bean内属性 --> <s:property value="#person.name"/><br/> </s:iterator>
(4)测试:
(5)使用status获取更多信息:
8.<b>iterator</b>标签:<br/> <s:iterator value="persons" var="person" status="sau" > <!-- 此时须#开头访问变量 --> 元素:<s:property value="#person"/><br/> 元素name属性:<s:property value="#person.name"/><br/> <!-- 使用status获取当前属性的信息 --> 索引index:<s:property value="#sau.index"/><br/> 座次count:<s:property value="#sau.count"/><br/> 偶数even:<s:property value="#sau.even"/><br/> 奇数odd:<s:property value="#sau.odd"/><br/> 队首first:<s:property value="#sau.first"/><br/> 队尾last:<s:property value="#sau.last"/><br/> </s:iterator>
(6)测试:
(7)属性投影:
8.<b>iterator</b>标签:<br /> 投影,persons.{name},输出的只有name:<br/> <s:iterator value="persons.{name}" var="person" status="sau"> <s:property value="#person" /> <br /> </s:iterator>
(8)测试:
(9)属性过滤:
8.<b>iterator</b>标签:<br /> 过滤,(1)persons.{?#this.age>23},输出的只有age大于23的:<br/> <s:iterator value="persons.{?#this.age>23}" var="person" status="sau"> <s:property value="#person.name" /> <br /> </s:iterator> (2)persons.{^#this.age>23},输出的只有第一条记录:<br/> <s:iterator value="persons.{^#this.age>23}" var="person" status="sau"> <s:property value="#person.name" /> <br /> </s:iterator> (3)persons.{$#this.age>23},输出的只有最后一条记录:<br/> <s:iterator value="persons.{$#this.age>23}" var="person" status="sau"> <s:property value="#person.name" /> <br /> </s:iterator> 先过滤后投影:<br/> <s:iterator value="persons.{?#this.age>22}.{name}" var="person" status="sau"> <s:property value="#person" /> <br /> </s:iterator>
(10)测试:
(1)success.jsp:
8.<b>iterator</b>标签:<br /> <s:iterator value="persons" var="person" status="sau"> 元素name属性:<s:property value="#person.age" /> <br /> <s:if test="#person.age<22"> <!-- 使用status获取当前属性的信息 --> 索引index:<s:property value="#sau.index" /> <br /> 座次count:<s:property value="#sau.count" /> <br /> </s:if> <s:elseif test="#person.age<23"> 年龄大于等于22,小于23.<br/> </s:elseif> <s:else> 年龄大于等于23<br/> </s:else> </s:iterator>
(2)测试:
(1)struts.xml中的配置:
(2)success.xml:
9.<b>url</b>标签:<br/> 使用url配置网络地址:<br/> <s:url namespace="/pkgOgnl" action="ActionValueStack_test" var="ognl_action"> <!-- 可以根据需要配置参数,可以多个。只能value=数值 --> <s:param name="age" value="12"></s:param> <s:param name="parm" value="455"></s:param> </s:url> <!-- 配置以后需要在其它地方引用才会生效 --> <a href="<s:property value='#ognl_action'/>">测试strtus中ognl的url标签</a> <br/>
(3): 测试:
<%@ taglib uri="/struts-tags" prefix="s" %> <body> <!-- 表单。namespace=“struts.xml中包的namespace” action=“struts.xml中配置的action的name” method=“post/get” theme="表单格式",优先级高于struts.xml中的常量 simple为最精简html代码格式,s:form中的label将失效; xhtml为使用表格自动排版; css_xhtml为使用css排版; ajax为增加一些ajax的功能 --> <s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post" theme="simple"> <!-- UI标签 --> </s:form> </body>
<s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post"> <!-- 文本框。 value="默认值" label="框前显示文本",因 theme="simple"所以label失效 --> 文本框:<s:textfield id="s_tf_id" name="s_tf_name" value="default" label="文本框"></s:textfield> </s:form>
<s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post"> <!-- 密码框。value=“密码内容” label="显示文本" showPassword=“是否显示密码,默认false” 单独配置theme。优先级高于s:form中的theme --> <s:password id="s_pw_id" name="s_pw_name" value="369122" label="密码框" showPassword="true" theme="xhtml"></s:password> </s:form>
<s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post"> <!-- 隐藏区。value=“隐藏值” --> <s:hidden id="s_hid_id" name="s_hid_name" value="value_hidden"></s:hidden> </s:form>
<s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post"> <s:submit type="input" id="s_sb_inputid" name="s_sb_inputname" value="提交"></s:submit> <s:submit type="button" id="s_sb_buttonid" name="s_sb_buttonname" value="按钮"></s:submit> <s:submit type="image" id="s_sb_imageid" name="s_sb_imagename" value="图片"></s:submit> </s:form>
<s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post"> <!-- 取消与重置 --> <s:reset type="button" id="s_r_buttonid" name="s_r_buttonname" value="取消"></s:reset> <s:reset type="input" id="s_r_inputid" name="s_r_inputname" value="重置"></s:reset> </s:form>
<s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post"> <!-- 文本域 。theme="simple"所以label无效了 rows="行" cols="列" value="默认值" readonly="只读true/false" --> 文本域:<s:textarea id="s_ta_id" name="s_ta_name" label="文本域" rows="6" cols="30" value="默认值"></s:textarea> </s:form>
<s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post"> <!-- 复选框。fieldValue="发送到服务器的实际值" --> <s:checkbox id="s_cb_id" name="s_cb_name" label="复选1" fieldValue="fv" theme="xhtml"/> </s:form>
<s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post"> <!-- 复选框序列。list(MAP集合)="\#{'key值':'value值', key是发送到服务器的数据:value是页面显示文本'}" --> <s:checkboxlist id="s_cbl_id1" name="s_cbl_name1" label="复选框Map序列" listKey="key" listValue="value" theme="xhtml" list="#{ '01':'Java', '02':'Linux', '03':'Oracle' }"></s:checkboxlist> <!-- 复选框序列。list(LIST集合)="{'value值', 'value是页面显示文本,也是发送到服务器的数据值'}" --> <s:checkboxlist id="s_cbl_id2" name="s_cbl_name2" label="复选框List序列" theme="xhtml" list="{ 'Java', 'Linux', 'Oracle', 'Object-C' }" value="{'Linux','Oracle'}"></s:checkboxlist> </s:form>
<s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post"> <!-- 单选框。操作MAP。listKey和listValue同checkboxlist,可选 --> <s:radio id="s_rd_id1" name="s_rd_name1" label="单选框-MAP" list="#{ '01':'Java', '02':'Linux', '03':'Oracle' }" listKey="key" listValue="value" /> <!-- 单选框。操作LIST --> <s:radio id="s_rd_id2" name="s_rd_name2" label="单选框-LIST" list="{ 'Java', 'Linux', 'Oracle' }"></s:radio> </s:form>
<s:form id="s_form_id" name="s_form_name" namespace="/pkgUI" action="ActionUI_test" method="post"> <!-- 下拉框。操作MAP --> <s:select id="s_sl_id1" name="s_sl_name1" label="下拉框-MAP" list="#{ '01':'Java', '02':'Linux', '03':'Oracle' }" listKey="key" listValue="value" /> <!-- 下拉框。操作LIST --> <s:select id="s_sl_id2" name="s_sl_name2" label="下拉框-LIST" list="{ 'Java', 'Linux', 'Oracle', 'Object-C' }" value="'Linux'"></s:select> </s:form>