struts2值栈学习

来源:互联网 发布:淘宝固定背代码 编辑:程序博客网 时间:2024/06/06 09:15

1:简单的说,值栈是对应每一个请求对象的轻量级的数据存储中心,在这里统一管理着数据,供Action、Result、Interceptor等Struts2的其他部分使用,这样数据被集中管理起来而不凌乱。

      简单的说,值栈能够线程安全的为每个请求提供公共的数据存取服务。

      当有请求的时候,Struts2会为每个请求创建一个新的值栈,也就是说,栈和请求是一一对应的,不同的请求,值栈也不一样,而值栈封装了一次请求所有需要操作的相关的数据。

     正是因为值栈和请求的对应关系,因此值栈能保证线程安全的为每个请求提供公共的数据存取服务。


2:狭义值栈

     通常是指com.opensymphony.xwork2.util.ValueStack接口的对象,目前就是com.opensymphony.xwork2.ognl.OgnlValueStack对象。

     狭义值栈主要用来存放动态EL(表达式语言)运算需要的值和结果,当然OgnlValueStack对象主要是用来支持OGNL(对象图导航语言)运算的。

     狭义值栈中存放着一些OGNL可以访问的数据,如下:

        a:action的实例,这样就可以通过OGNL来访问Action实例中的属性的值了。

        b:OGNL表达式运算的值,可以设置到值栈中,可以主动访问值栈对象,强行设置。

        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

  

[html] view plaincopy
  1. import com.opensymphony.xwork2.ActionInvocation;  
  2. import com.opensymphony.xwork2.interceptor.PreResultListener;  
  3.   
  4. public class MyPreResultListener implements PreResultListener {  
  5.   
  6.     @Override  
  7.     public void beforeResult(ActionInvocation invocation, String resultCode) {  
  8.         System.out.println("现在处理Result执行前的功能, result=" + resultCode);  
  9.           
  10.         //在Result处理之前修改value stack里面的username对应的值  
  11.         invocation.getInvocationContext().getValueStack().setValue("username", "被修改了");  
  12.   
  13.     }  
  14.   
  15. }  

然后在相应的Action中进行注册:

  

[java] view plaincopy
  1. import com.capinfotech.listener.MyPreResultListener;  
  2. import com.opensymphony.xwork2.ActionContext;  
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class PreResultAction extends ActionSupport {  
  6.   
  7.     private String username;  
  8.     private String password;  
  9.   
  10.     public String getUsername() {  
  11.         return username;  
  12.     }  
  13.   
  14.     public void setUsername(String username) {  
  15.         this.username = username;  
  16.     }  
  17.   
  18.     public String getPassword() {  
  19.         return password;  
  20.     }  
  21.   
  22.     public void setPassword(String password) {  
  23.         this.password = password;  
  24.     }  
  25.       
  26.     public String execute() {  
  27.         System.out.println("用户输入的参数为,username:" + username + ", password:" + password);  
  28.           
  29.         ActionContext context = ActionContext.getContext();  
  30.         MyPreResultListener preListener = new MyPreResultListener();  
  31.         context.getActionInvocation().addPreResultListener(preListener);  
  32.           
  33.         return "success";  
  34.           
  35.     }  
  36.   
1 0