标签:session ddd 表单 class tls tco new form image
OGNL(Object Graphic Navigatino Language)
OGNL称为对象图导航语言。以一个对象为根(起点),通过OGNL可以访问与这个对象关联的其它对象,正所谓“对象导航,顺藤摸瓜”
使用OGNL的jsp需要引入标签库:<%@taglib prefix="s" uri="/struts-tags"%>
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="p1" namespace="/" extends="struts-default"> <action name="m1Action" class="org.ah.s2.C1" method="m1"> <result name="success" type="dispatcher"> /inputMsg.jsp </result> </action> </package> </struts>
模型层:来个两级模型
Model.java
package org.ah.s2; public class Model { private String uname; private M2 m2; public Model(String uname, M2 m2) { this.uname = uname; this.m2 = m2; } public M2 getM2() { return m2; } public void setM2(M2 m2) { this.m2 = m2; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } }
M2.java
package org.ah.s2; public class M2 { public M2(String name2) { this.name2 = name2; } private String name2; public String getName2() { return name2; } public void setName2(String name2) { this.name2 = name2; } }
Action:C1.java
package org.ah.s2; import java.util.ArrayList; import java.util.List; import org.apache.struts2.ServletActionContext; import ognl.Ognl; import ognl.OgnlException; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ValueStack; public class C1 extends ActionSupport { private Model m; private List<Model> lstM; // ------------------------------------------------------------------ public Model getM() { return m; } public void setM(Model m) { this.m = m; } public List<Model> getLstM() { return lstM; } public void setLstM(List<Model> lstM) { this.lstM = lstM; } // ------------------------------------------------------------------ public String m1() { ActionContext ac = ActionContext.getContext(); // 放入值栈外围 ac.put("msg", "out_value"); // 放入值栈 ValueStack valueStack = ac.getValueStack(); valueStack.set("msg", "vStack_value");// OK // 放入Application ac.getApplication().put("msg", "hello application"); // 放入session ac.getSession().put("msg", "hello session"); // 放入request(上面的ActionContext ac搞不定,需要ServletActionContext) ServletActionContext.getRequest().setAttribute("msg", "hello request"); // 放置对象 m = new Model("吕布", new M2("方天画戟")); // 放置List Model m1 = new Model("关羽", new M2("青龙偃月刀")); Model m2 = new Model("张飞", new M2("丈八蛇矛枪")); Model m3 = new Model("赵云", new M2("龙胆亮银枪")); lstM = new ArrayList<Model>(); lstM.add(m1); lstM.add(m2); lstM.add(m3); return "success"; } }
index.jsp核心代码
<s:form action="m1Action" method="post"> <s:textfield name="msg" value="my input"></s:textfield> <s:submit value="测试Struts控件传值"></s:submit> </s:form>
回显页inputMsg.jsp核心代码
1.值栈中的值: <s:property value="msg" /> <hr> 2.值栈外围值: <s:property value="#msg" /> <br> 3. <s:property value="#parameters.msg" />取表单提交值 <br> 4. <s:property value="#application.msg" /> <br> 5. <s:property value="#session.msg" /> <br> 6. <s:property value="#request.msg" /> <br> 7. <s:property value="#attr.msg" /> :按request > session > application顺序访问其属性 <br> 8.取对象 <s:property value="m.uname" /> <s:property value="m.m2.name2" /> <br> 9.取List <!-- 用listValue指定显示的内容 --> 下拉列表: <s:select list="lstM" listValue="uname" headerValue="缺省值"> </s:select> <br> 下拉列表(自定义List): <s:select list="#{‘0‘:‘男‘,‘1‘:‘女‘}" label="性别" headerValue="请选择性别" /> <br> <br> 循环遍历: <br> <s:iterator value="lstM" var="per" status="sta"> <s:property value="#per.uname" /> <s:property value="#per.m2.name2" /> <br> 循环的次数为:<s:property value="#sta.count" /> <br> 循环的索引为:<s:property value="#sta.index" /> <br> 是否为奇数:<s:property value="#sta.odd" /> <s:if test="#sta.count==1">No.1</s:if> <s:elseif test="#sta.count==2">No.2</s:elseif> <s:else>No.3</s:else> <br /> </s:iterator> <br> 遍历set集合:(自定义的字符串不能是字符!至少加一个空格) <br> <s:set name="set" value="{‘E ‘,‘ddddd‘,‘ccccc‘,‘bbbbb‘,‘aaaaa‘}"></s:set> <s:iterator value="#set" var="o"> <!-- ${o }<br/> --> <s:property value="o" /> <br /> </s:iterator>
<s:debug></s:debug>
直接点击提交,查看运行结果:
标签:session ddd 表单 class tls tco new form image
原文地址:http://www.cnblogs.com/AndyHoo/p/6241482.html