1:简单的说,值栈是对应每个请求对象的轻量级的数据存储中心,在这里统一管理着数据,供Action、Result、Interceptor等Struts2的其余部分使用,这样数据被集中管理起来而不凌乱。html
简单的说,值栈可以线程安全的为每一个请求提供公共的数据存取服务。java
当有请求的时候,Struts2会为每一个请求建立一个新的值栈,也就是说,栈和请求是一一对应的,不一样的请求,值栈也不同,而值栈封装了一次请求全部须要操做的相关的数据。express
正是由于值栈和请求的对应关系,所以值栈能保证线程安全的为每一个请求提供公共的数据存取服务。apache
2:狭义值栈api
一般是指com.opensymphony.xwork2.util.ValueStack接口的对象,目前就是com.opensymphony.xwork2.ognl.OgnlValueStack对象。安全
狭义值栈主要用来存放动态EL(表达式语言)运算须要的值和结果,固然OgnlValueStack对象主要是用来支持OGNL(对象图导航语言)运算的。session
狭义值栈中存放着一些OGNL能够访问的数据,以下:app
a:action的实例,这样就能够经过OGNL来访问Action实例中的属性的值了。less
b:OGNL表达式运算的值,能够设置到值栈中,能够主动访问值栈对象,强行设置。jsp
c:OGNL表达式产生的中间变量,好比使用Struts2标签的时候,使用循环标签,天然会有循环的变量,这些都放在值栈中。
3:广义值栈
一般是ActionContext对象,ActionContext是Action运行的上下文,每一个ActionContext是一个基本的容器,包含着Aciton运行须要的数据,好比请求参数,会话等。
ActionContext也是线程安全的,每一个线程都有一个独立的ActionContext,这样就不用担忧值栈中值得线程安全问题了。
ActionContext里面存储着不少值,以下:
a:Request的Parameters,请求中的参数,注意这里的数据是从数据对象中复制来的,所以这里的数据的变化是不会影响到请求对象里面的参数的值的。
b:Request的Attribute,请求中的属性,这里是一个Map,存放着请求对象的属性数据,这些数据和请求对象的Attribute是联动的。
c:Application的Attribute,应用的属性,这里是一个Map,存放着应用对象的属性数据,这些数据和应用对象的attribute是联动的。
d:ValueStack,也就是狭义值栈,ActionContext是以value stack做为被OGNL访问的根,简单的说,OGNL在没有特别指明的状况下,访问的就是value stack的值。
e:attr,在全部的属性范围中获取值,依次搜索page, request, session 和applicaion
4:ActionContext的使用
获取,经过两种方式,第一种,使用ActionContext自身的方法来获取
ActionContext ctx = ActionContext.getContext();
第二种,使用ActionInvocation来获取
ActionContext ctx = actionInvocation.getInvocationContext();
它的典型方法以下:
Objectget(String key):Returns a value that is stored in the current ActionContext by doing a lookup using the value's key.
void put(String key,Object value):Stores a value in the current ActionContext.
Map<String,Object>getApplication(): Returns a Map of the ServletContext when in a servlet environment or a generic application level Map otherwise.
即返回ServletContext中返回的值
Map<String,Object>getSession():Gets the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
Map<String,Object>getContextMap():Gets the context map.
Map<String,Object>getParameters(): Returns a Map of the HttpServletRequest parameters when in a servlet environment or a generic Map of parameters otherwise.
5:ValueStack的使用
ValueStack有一个特色,若是访问的值栈里有多个对象,且相同的属性在多个对象中同时出现,则值栈会按照从栈顶到栈底的顺序,寻找第一个匹配的对象。
ValueStack的获取:直接由ActionContext对象的getValueStack()方法便可得到。
使用函数:
ObjectfindValue(String expr): Find a value by evaluating the given expression against the stack in the default search order. voidsetValue(String expr,Object value): Attempts to set a property on a bean in the stack with the given expression using the default search order. Object peek():Get the object on the top of the stack without changing the stack. Objectpop():Get the object on the top of the stack and remove it from the stack. voidpush(Object o):Put this object onto the top of the stack
6:例子,修改用户输入的参数信息,以下图所示,
图:用户输入了aa的username
图:用户提交后发现username属性的值发生了变化
实现:
首先定义一个实现PreResultListener接口的类:MyPreResultListener