用户每次访问struts的action,都会创建一个Action对象、值栈对象、ActionContext对象;然后把Action对象放入值栈中; 最后再把值栈对象放入request中,传入jsp页面(key: struts.valueStack)。
值栈的内存结构分为:
map栈:
request
session
application
list栈:
action及全局对象
我们只需要通过ActionContext对象就可以访问struts的其他的关键对象。
public class OgnlDemo2 extends ActionSupport { private User user = new User(100,"Jacks"); public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { // TODO Auto-generated method stub ActionContext ac = ActionContext.getContext(); ac.put("test", new User(222, "Ronaldo")); ValueStack vs = ac.getValueStack(); vs.push(new User(111, "Asen")); return SUCCESS; } }
struts会将全局属性及action对象放入根元素中即list栈,如图:
以下方法可以操作该栈对象
vs.push(new User(101, "Asen"));//可以list栈顶添加对象
vs.pop();//可以移除list栈顶对象
vs.set("new_user", new User(222, "Ronaldo"));//也可以往list栈添加元素
ac.getContextMap().put("user1", new User(111, "mapUser"));
ac.getSession().put("user2", new User(444, "sessionUser"));
ac.getApplication().put("user3", new User(5555, "applicationUser"));
可以向map栈(OgnlContext)添加对象
从这个table里可以找到:
user1=com.asen.test4.entity.User@7dbeb971
session={user2=com.asen.test4.entity.User@59713249}
application={user3=com.asen.test4.entity.User@9482b33
综上所述:
ActionContext.getContext().put(String key, Object value)放入map栈(OgnlContext)
ActioinContext.getContext().getValueStack().push(Object value)放入list栈
在jsp页面我们可以通过struts标签获取值栈里的对象
对于map栈的数据,加#号访问
<s:iterator value="#test"> <tr> <td><s:property value="id" /></td> <td><s:property value="name" /></td> </tr> </s:iterator>
ognl表达式对于list栈的数据直接根据key值提取value值就可以了
<s:property value="id" />
还可以用
<td><s:textarea name="id" /></td>
<h2>ValueStack vs = ac.getValueStack();</h2> <h2>vs.push(new User(111, "Asen"));</h2> <tr> <td><s:property value="id" /></td> <td><s:property value="name" /></td> </tr> <tr> <td><s:textfield name="id" /></td> <td><s:textfield name="name" /></td> </tr> <h2>private User user = new User(100,"Jacks");</h2> <tr> <td><s:property value="user.id" /></td> <td><s:property value="user.name" /></td> </tr>
以上是前端页面获取到数据,接下来是前端页面提交数据到服务器,服务器接收数据
假如有一个用来处理客户的 CustomerAction 类, 该动作类实现了 ModelDriven 接口,当用户触发CustomerAction 动作时, ModelDriven 拦截器将调用相关CustomerAction 对象的 getModel() 方法, 并把返回的模型(Customer实例)压入到 ValueStack 栈. 接下来 Parameters 拦截器将把表单字段映射到 ValueStack 栈的栈顶对象的各个属性中. 因为此时 ValueStack 栈的栈顶元素是刚被压入的模型(Product)对象, 所以该模型将被填充. 如果某个字段在模型里没有匹配的属性, Param 拦截器将尝试 ValueStack 栈中的下一个对象.
Department department = new Department(); /** * 把model的值赋值给了department */ BeanUtils.copyProperties(this.getModel(), department);
相关推荐
struts2--3.Ognl与值栈
struts2标签使用例子 struts2标签使用例子 struts2标签使用例子struts2标签使用例子
本资源适合刚接触Struts和Ognl的小伙伴交流学习,欢迎吐槽。
有对struts2标签和OGNL的总结。
ognl.jar包,struts2开发必备。用以支持OGNL表达式的。
3.由于ValueStack(值栈)是Struts 2中OGNL的根对象,如果用户需要访问值栈中的对象,在JSP页面可以直接通过下面的EL表达式访问ValueStack(值栈)中对象的属性 4.为何使用EL表达式能够访问valueStack中对象的属性 原因...
从官网checkout下来的源码,如果大家觉的区官网麻烦可以直接下载这个
struts标签和ognl语法,比较全的标签说明
struts2 OGNL的主要用法 基本覆盖全部
1、什么是值栈 2、值栈的内部结构 3、ActionContext和ValueStatck的关系 4、如何获取值栈对象 5、向值栈存数据 6、从值栈中获取数据
struts2中siterator 标签的使用详解 及 OGNL用法 struts2中siterator 标签的使用详解 及 OGNL用法
Struts2 OGNL---标签详解析 都有实例 适合初学者
Struts2的标签库及OGNL表达式语言
struts2的ognl
struts2标签和OGNL表达式struts2标签和OGNL表达式
Struts2学习计划5---Struts2_OGNL(对象图导航语言)的学习
OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言。是一种可以方便地操作对象属性的开源表达式语言。OGNL有如下特点: 1、支持对象方法调用,形式如:objName.methodName(); 2、支持...
struts2 OGNL之标签访问值栈(value stack)用法,希望能对大家有帮助
struts2 中 OGNL表达式的使用struts2 中 OGNL表达式的使用
struts2 OGNL,struts2 表达式语言,Struts2 中OGNL表达式的用法,Struts2 #,表达式语言的好处,Struts2 $,struts2 井号,星号,百分号