值栈

来源:互联网 发布:东华软件个股分析 编辑:程序博客网 时间:2024/06/02 04:53

目录(?)[+]

    笔者不知道该用哪个词来形容ValueStack、ActionContext等可以在Struts2中用来存放数据的类。这些类使用的范围不同,得到的方法也不同,下面就来一一介绍。

    声明:本文参考Struts2版本为2.3.1.2,内容仅供参考,限于笔者水平有限,难免有所疏漏,望您能友善指出。本文发表于ITEYE,谢绝转载。

1. ValueStack

    ValueStack在中文版的《Struts2深入浅出》一书中译作“值栈”。其本身数据结构是一个栈,使用者可以把一些对象(又称作bean)存入值栈中,然后使用动态的表达式来读取bean的属性,或者对bean进行一些其他操作。由于值栈中可能有多个bean,值栈会按bean出栈的顺序依次尝试使用动态的表达式来读取值,直到成功读取值为止。在Struts2中,默认的值栈实现是OgnlValueStack,即默认使用Ognl这个动态表达式语言来读取值。

    在Struts2执行一次请求的过程中,Struts2会把当前的Action对象自动放入值栈。这样,在渲染JSP时,JSP里的代码使用<s:property value="..."/>之类标签中的Ognl表达式会直接作用于Action对象,从而方便的读取Action的属性。

     如何得到值栈:
  • 在自定义的拦截器中,使用ActionInvocation.getStack()方法( ActionInvocation 是拦截器的方法参数)。
  • 在Action类中,让拦截器注入ValueStack或者使用ActionContext.getContext().getValueStack()来值栈(ActionContext.getContext()为静态方法)。注意:ActionContext分配context的方式是基于线程的,如果使用这种方法,请确保它不会出错。
  • 在JSP中,直接使用标签即可获得值栈里的数据,而一般不用获取值栈本身。
    如何将对象存入值栈:
  • Struts2自动存入Action:之前已经提到,Struts2在执行一次请求的过程中会把当前的Action对象自动存入值栈中。
  • ModelDrivenInterceptor会存入Action的model属性:如果你使用了Struts2提供的 ModelDrivenInterceptor,则它会把Action对象的getModel()方法得到的对象存入值栈中。此时,值栈最底层为Action类,其次为这个model。
  • 在自定义的拦截器中存入值栈:得到值栈对象后调用ValueStack.put(Object object)方法。
  • 在Action类中存入值栈:得到值栈对象后调用ValueStack.put(Object object)方法。
  • 在JSP中存入值栈:标签<s:push value="..."></s:push>是专门用来在JSP中把指定的value放入值栈的,但value被放入值栈的时间仅在s:push标签内,即程序运行到</s:push>标签处会把value从值栈中移出。另外,还有一些标签比如<s:iterator/>由于其功能的需要也会把一些对象放到值栈中。
    让值栈执行表达式来获得值:
  • 在自定义的拦截器中,获得值栈后,使用ValueStack.findValue(...)等方法。
  • 在Action类中,获得值栈后,使用ValueStack.findVlaue(...)等方法。
  • 在JSP中,一些标签的属性是直接在值栈上执行Ognl表达式的,比如<s:property/>的value属性。如果标签的属性不是直接执行Ognl表达式的,则需要使用“%{}”将表达式括起来,这样Struts2就会以Ognl表达式来执行了。至于到底哪些标签是直接执行Ognl而哪些不是,请参考完整的官方文档。
    在JSP中跳过栈顶元素直接访问第二层:
  • 在JSP中,使用[0]、[1]等表达式来指定从栈的第几层开始执行表达式。[0]表示从栈顶开始,[1]表示从栈的第二层开始。比如表达式“name”等价于“[0].name”。参见此处。
    在JSP中访问值栈对象本身(而不是它们的属性)
  • 在表示式中使用top关键字来访问对象本身。比如,表达式“name”等价于“top.name”,表达式“[0].top”等价于“top”,表达式“[1].top.name”等价于“[1].name”。

    总之,值栈主要目的是为了让JSP内能方便的访问Action的属性。


    一些例子:

Java代码 复制代码 收藏代码
  1. // 此类为一个封装数据的简单类,在下面的例子会用到   
  2. public class Person {   
  3.   
  4.     private String name;   
  5.   
  6.     public String getName() {   
  7.         return name;   
  8.     }   
  9.   
  10.     public void setName(String name) {   
  11.         this.name = name;   
  12.     }   
  13. }  
[java] view plain copy
  1. // 此类为一个封装数据的简单类,在下面的例子会用到  
  2. public class Person {  
  3.   
  4.     private String name;  
  5.   
  6.     public String getName() {  
  7.         return name;  
  8.     }  
  9.   
  10.     public void setName(String name) {  
  11.         this.name = name;  
  12.     }  
  13. }  
 

Java代码 复制代码 收藏代码
  1. // 本类将演示拦截器中对值栈的操作   
  2. public class MyInterceptor extends AbstractInterceptor {   
  3.   
  4.     public String intercept(ActionInvocation invocation) throws Exception {   
  5.         // 获得值栈   
  6.         ValueStack valueStack = invocation.getStack();   
  7.         // 存入值   
  8.         Person person = new Person();   
  9.         valueStack.push(person);   
  10.         // 执行表达式获取值   
  11.         String name = (String) valueStack.findValue("name");   
  12.         // 其他代码   
  13.         return invocation.invoke();   
  14.     }   
  15. }  
[java] view plain copy
  1. // 本类将演示拦截器中对值栈的操作  
  2. public class MyInterceptor extends AbstractInterceptor {  
  3.   
  4.     public String intercept(ActionInvocation invocation) throws Exception {  
  5.         // 获得值栈  
  6.         ValueStack valueStack = invocation.getStack();  
  7.         // 存入值  
  8.         Person person = new Person();  
  9.         valueStack.push(person);  
  10.         // 执行表达式获取值  
  11.         String name = (String) valueStack.findValue("name");  
  12.         // 其他代码  
  13.         return invocation.invoke();  
  14.     }  
  15. }  
 
Java代码 复制代码 收藏代码
  1. // 本类将演示在Action中对值栈进行操作   
  2. public class MyAction extends ActionSupport {   
  3.   
  4.     @Override  
  5.     public String execute() throws Exception {   
  6.         // 获得值栈   
  7.         ValueStack valueStack = ActionContext.getContext().getValueStack();   
  8.         // 存入值   
  9.         Person person = new Person();// 这是之前例子中定义的类  
  10.         valueStack.push(person);   
  11.         // 执行表达式获取值   
  12.         String name = (String) valueStack.findValue("name");   
  13.         // 其他代码   
  14.         // ......   
  15.         return SUCCESS;   
  16.     }   
  17.     // 以下定义的属性供接下来的JSP例子使用   
  18.     private String message;   
  19.     private Person person;   
  20.     private List<Person> personList;   
  21.   
  22.     public String getMessage() {   
  23.         return message;   
  24.     }   
  25.   
  26.     public Person getPerson() {   
  27.         return person;   
  28.     }   
  29.   
  30.     public List<Person> getPersonList() {   
  31.         return personList;   
  32.     }   
  33. }  
[java] view plain copy
  1. // 本类将演示在Action中对值栈进行操作  
  2. public class MyAction extends ActionSupport {  
  3.   
  4.     @Override  
  5.     public String execute() throws Exception {  
  6.         // 获得值栈  
  7.         ValueStack valueStack = ActionContext.getContext().getValueStack();  
  8.         // 存入值  
  9.         Person person = new Person();// 这是之前例子中定义的类  
  10.         valueStack.push(person);  
  11.         // 执行表达式获取值  
  12.         String name = (String) valueStack.findValue("name");  
  13.         // 其他代码  
  14.         // ......  
  15.         return SUCCESS;  
  16.     }  
  17.     // 以下定义的属性供接下来的JSP例子使用  
  18.     private String message;  
  19.     private Person person;  
  20.     private List<Person> personList;  
  21.   
  22.     public String getMessage() {  
  23.         return message;  
  24.     }  
  25.   
  26.     public Person getPerson() {  
  27.         return person;  
  28.     }  
  29.   
  30.     public List<Person> getPersonList() {  
  31.         return personList;  
  32.     }  
  33. }  

  
Html代码 复制代码 收藏代码
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
  2. <%@taglib uri="/struts-tags" prefix="s" %>  
  3. <!DOCTYPE html>  
  4. <html>  
  5.     <head>  
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7.         <title>JSP Page</title>  
  8.     </head>  
  9.     <body>  
  10.         <!-- 本JSP将演示在JSP中对值栈的使用 -->  
  11.         <!-- 本JSP为MyAction对应的JSP -->  
  12.   
  13.         <!-- 由于Action已经被存入的值栈,所以可以调用Action的属性  -->  
  14.         <!-- 使用下面的标签和表达式来显示MyAction的message属性 -->  
  15.         <s:property value="message"/>  
  16.         <!-- 使用下面的标签和表达式来调用Action的getText(...)方法,参数为MyAction的message属性  -->  
  17.         <s:property value="getText(message)"/>  
  18.         <!-- 默认情况下传递给cssClass的是字符串常量。可以使用“%{}”来启用Ognl,这样,传递给cssClass的就不是字符串常量"message",而是上面所说的message的值  -->  
  19.         <s:div cssClass="%{message}"/>  
  20.         <!-- 使用s:push标签来将对象放入值栈,如下 -->  
  21.         <s:push value="person">  
  22.             <!-- 在此s:push标签内,值栈的栈顶元素为person,栈顶第二层为action   
  23.             <!-- 在标签内直接调用person的属性(而不是Action的属性),如下 -->  
  24.             <s:property value="name"/>  
  25.             <!-- 在标签内也可以使用MyAction的属性,值栈会依次先查找Person是否有该属性,由于没找到,会再MyAction中再查找,如下  -->  
  26.             <s:property value="message"/>  
  27.             <!-- 可以使用“[0]”、“[1]”等指定从值栈的哪一层开始查找  -->  
  28.             <!-- 此时,使用“[0]”表示从Person开始查找,当然还是找不到,值栈就接着到MyAction中查找,如下  -->  
  29.             <s:property value="[0].message"/>  
  30.             <!-- 此时,使用“[1]”将从MyAction开始查找,而跳过了person,如下  -->  
  31.             <s:property value="[1].message"/>  
  32.             <!-- 想要访问栈顶元素本身使用关键字“top”,比如,下面的top就代表栈顶的person,如下 -->  
  33.             <s:property value="top"/>  
  34.             <!-- 或者如下  -->  
  35.             <s:property value="[0].top"/>  
  36.             <!-- 想要访问MyAction本身的话使用如下写法  -->  
  37.             <s:property value="[1].top"/>  
  38.         </s:push>  
  39.         <!-- 此时person已被移出值栈,再使用如下标签和表达式将无法得到值 -->  
  40.         <!--<s:property value="name"/>-->  
  41.         <!-- iterator标签会把list的每个元素依次存入栈顶,如下 -->  
  42.         <s:iterator value="personList">  
  43.             <!-- 获得List每个元素中的name属性  -->  
  44.             <s:property value="name"/>  
  45.         </s:iterator>  
  46.     </body>  
  47. </html>  
[html] view plain copy
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
  2. <%@taglib uri="/struts-tags" prefix="s" %>  
  3. <!DOCTYPE html>  
  4. <html>  
  5.     <head>  
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7.         <title>JSP Page</title>  
  8.     </head>  
  9.     <body>  
  10.         <!-- 本JSP将演示在JSP中对值栈的使用 -->  
  11.         <!-- 本JSP为MyAction对应的JSP -->  
  12.   
  13.         <!-- 由于Action已经被存入的值栈,所以可以调用Action的属性  -->  
  14.         <!-- 使用下面的标签和表达式来显示MyAction的message属性 -->  
  15.         <s:property value="message"/>  
  16.         <!-- 使用下面的标签和表达式来调用Action的getText(...)方法,参数为MyAction的message属性  -->  
  17.         <s:property value="getText(message)"/>  
  18.         <!-- 默认情况下传递给cssClass的是字符串常量。可以使用“%{}”来启用Ognl,这样,传递给cssClass的就不是字符串常量"message",而是上面所说的message的值  -->  
  19.         <s:div cssClass="%{message}"/>  
  20.         <!-- 使用s:push标签来将对象放入值栈,如下 -->  
  21.         <s:push value="person">  
  22.             <!-- 在此s:push标签内,值栈的栈顶元素为person,栈顶第二层为action  
  23.             <!-- 在标签内直接调用person的属性(而不是Action的属性),如下 -->  
  24.             <s:property value="name"/>  
  25.             <!-- 在标签内也可以使用MyAction的属性,值栈会依次先查找Person是否有该属性,由于没找到,会再MyAction中再查找,如下  -->  
  26.             <s:property value="message"/>  
  27.             <!-- 可以使用“[0]”、“[1]”等指定从值栈的哪一层开始查找  -->  
  28.             <!-- 此时,使用“[0]”表示从Person开始查找,当然还是找不到,值栈就接着到MyAction中查找,如下  -->  
  29.             <s:property value="[0].message"/>  
  30.             <!-- 此时,使用“[1]”将从MyAction开始查找,而跳过了person,如下  -->  
  31.             <s:property value="[1].message"/>  
  32.             <!-- 想要访问栈顶元素本身使用关键字“top”,比如,下面的top就代表栈顶的person,如下 -->  
  33.             <s:property value="top"/>  
  34.             <!-- 或者如下  -->  
  35.             <s:property value="[0].top"/>  
  36.             <!-- 想要访问MyAction本身的话使用如下写法  -->  
  37.             <s:property value="[1].top"/>  
  38.         </s:push>  
  39.         <!-- 此时person已被移出值栈,再使用如下标签和表达式将无法得到值 -->  
  40.         <!--<s:property value="name"/>-->  
  41.         <!-- iterator标签会把list的每个元素依次存入栈顶,如下 -->  
  42.         <s:iterator value="personList">  
  43.             <!-- 获得List每个元素中的name属性  -->  
  44.             <s:property value="name"/>  
  45.         </s:iterator>  
  46.     </body>  
  47. </html>  
 

2.ActionContext

    ActionContext是Action的上下文,Struts2自动在其中保存了一些在Action执行过程中所需的对象,比如session, parameters, locale等。Struts2会根据每个执行HTTP请求的线程来创建对应的ActionContext,即一个线程有一个唯一的ActionContext。因此,使用者可以使用静态方法ActionContext.getContext()来获取当前线程的ActionContext,也正是由于这个原因,使用者不用去操心让Action是线程安全的。

    无论如何,ActionContext都是用来存放数据的。Struts2本身会在其中放入不少数据,而使用者也可以放入自己想要的数据。ActionContext本身的数据结构是映射结构,即一个Map,用key来映射value。所以使用者完全可以像使用Map一样来使用它,或者直接使用Action.getContextMap()方法来对Map进行操作。

    Struts2本身在其中放入的数据有ActionInvocation、application(即ServletContext)、conversionErrors、Locale、action的name、request的参数、HTTP的Session以及值栈等。完整的列表请参考它的Javadoc(本文附录有对它包含内容的讨论)。

    由于ActionContext的线程唯一和静态方法就能获得的特性,使得在非Action类中可以直接获得它,而不需要等待Action传入或注入。需要注意的是,它仅在由于request而创建的线程中有效(因为request时才创建对应的ActionContext),而在服务器启动的线程中(比如fliter的init方法)无效。由于在非Action类中访问其的方便性,ActionContext也可以用来在非Action类中向JSP传递数据(因为JSP也能很方便的访问它)。

 

   ValueStack与ActionContext的联系和区别:
  • 相同点:它们都是在一次HTTP请求的范围内使用的,即它们的生命周期都是一次请求。
  • 不同点:值栈是栈的结构,ActionContext是映射(Map)的结构。
  • 联系:ValueStack.getContext()方法得到的Map其实就是ActionContext的Map。查看Struts2的源代码可知(Struts2.3.1.2的org.apache.struts2.dispatcher.ng.PrepareOperations的第79行,createActionContext方法),在创建ActionContext时,就是把ValueStack.getContext()作为ActionContext的构造函数的参数。所以,ValueStack和ActionContext本质上可以互相获得。
  • 注意:在一些文档中,会出现把对象存入“stack's context”的字样,其实就是把值存入了ActionContext。所以在阅读这些文档时,要看清楚,到底是放入了栈结构(即值栈),还是映射结构(值栈的context,即ActionContext)。
    如何获得ActionContext:
  • 在自定义的拦截器中:使用ActionInvocation.getInvocationContext()或者使用ActionContext.getContext()。
  • 在Action类中:让拦截器注入或者使用ActionContext.getContext()。
  • 在非Action类中:让Action类传递参数、使用注入机制注入或者使用ActionContext.getContext()。注意:只有运行在request线程中的代码才能调用ActionContext.getContext(),否则返回的是null。
  • 在JSP中:一般不需要获得ActionContext本身。
    如何向ActionContext中存入值:
  • 在拦截器、Action类、非Action类等Java类中:使用ActionContext.put(Object key, Object value)方法。
  • 在JSP中:标签<s:set value="..."/>默认将值存入ActionContext中(当然,<s:set>标签还可以把值存到其他地方)。另外,许多标签都有var属性(以前用的是id属性,现在id属性已被弃用),这个属性能向ActionContext存入值,key为var属性的值,value为标签的value属性的值。(有些文档写的是向ValueStack的context存入值,其实是一样的)
    如何从ActionContext中读取值:
  • 在拦截器、Action类、非Action类等Java类中:使用ActionContext.get(Object key)方法。
  • 在JSP中:使用#开头的Ognl表达式,比如<s:property value="#name"/>会调用ActionContext.get("name")方法。注意:如果某标签的属性默认不作为Ognl表达式解析,则需要使用%{}把表达式括起来,于是就会出现类似“%{#name}的表达式”。(“#”的更多用途参见这里)
    总之,在JSP中使用ActionContext一方面是由于它是映射结构,另一方面是能读取Action的一些配置。当你需要为许多Action提供通用的值的话,可以让每个Action都提供getXXX()方法,但更好的方法是在拦截器或JSP模板中把这些通用的值存放到ActionContext中(因为拦截器或JSP模板往往通用于多个Action)。

    一些例子:
Java代码 复制代码 收藏代码
  1. // 本类将演示拦截器中对ActionContext的操作   
  2. public class MyInterceptor extends AbstractInterceptor {   
  3.   
  4.     public String intercept(ActionInvocation invocation) throws Exception {   
  5.         // 获得ActionContext   
  6.         ActionContext actionContext = invocation.getInvocationContext();   
  7.         // 存入值   
  8.         Person person = new Person();   
  9.         actionContext.put("person", person);   
  10.         // 获取值   
  11.         Object value = actionContext.get("person");   
  12.         // 获取HttpServletRequest   
  13.         HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);   
  14.         // 获取request的Map,即HttpServletRequest.getAttribute(...)和HttpServletRequest.setAttribute(...)所操作的值  
  15.         Map requestMap = (Map) actionContext.get("request");   
  16.         // 其他代码   
  17.         // ......   
  18.         return invocation.invoke();   
  19.     }   
  20. }  
[java] view plain copy
  1. // 本类将演示拦截器中对ActionContext的操作  
  2. public class MyInterceptor extends AbstractInterceptor {  
  3.   
  4.     public String intercept(ActionInvocation invocation) throws Exception {  
  5.         // 获得ActionContext  
  6.         ActionContext actionContext = invocation.getInvocationContext();  
  7.         // 存入值  
  8.         Person person = new Person();  
  9.         actionContext.put("person", person);  
  10.         // 获取值  
  11.         Object value = actionContext.get("person");  
  12.         // 获取HttpServletRequest  
  13.         HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);  
  14.         // 获取request的Map,即HttpServletRequest.getAttribute(...)和HttpServletRequest.setAttribute(...)所操作的值  
  15.         Map requestMap = (Map) actionContext.get("request");  
  16.         // 其他代码  
  17.         // ......  
  18.         return invocation.invoke();  
  19.     }  
  20. }  
 
Java代码 复制代码 收藏代码
  1. // 本类将演示在Action中对ActionContext进行操作   
  2. public class MyAction extends ActionSupport {   
  3.   
  4.     @Override  
  5.     public String execute() throws Exception {   
  6.         // 获得值栈   
  7.         ActionContext actionContext = ActionContext.getContext();   
  8.         // 存入值   
  9.         Person person = new Person();// 这是之前例子中定义的类  
  10.         actionContext.put("person", person);   
  11.         // 获取值   
  12.         Object object = actionContext.get("person");   
  13.         // 其他代码   
  14.         // ......   
  15.         return SUCCESS;   
  16.     }   
  17. }  
[java] view plain copy
  1. // 本类将演示在Action中对ActionContext进行操作  
  2. public class MyAction extends ActionSupport {  
  3.   
  4.     @Override  
  5.     public String execute() throws Exception {  
  6.         // 获得值栈  
  7.         ActionContext actionContext = ActionContext.getContext();  
  8.         // 存入值  
  9.         Person person = new Person();// 这是之前例子中定义的类  
  10.         actionContext.put("person", person);  
  11.         // 获取值  
  12.         Object object = actionContext.get("person");  
  13.         // 其他代码  
  14.         // ......  
  15.         return SUCCESS;  
  16.     }  
  17. }  
 
Html代码 复制代码 收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5.         <title>JSP Page</title>  
  6.     </head>  
  7.     <body>  
  8.         <!-- 本JSP将演示在JSP中对ActionContext的使用 -->  
  9.         <!-- 本JSP为MyAction对应的JSP -->  
  10.   
  11.         <!-- 由于Action中已经向ActionContext存入了key为"person"的值,所以可以使用“#person”来获取它,如下  -->  
  12.         <s:property value="#person"/>  
  13.         <!-- 获得person的name属性,如下  -->  
  14.         <s:property value="#person.name"/>  
  15.         <!-- 获得Struts2在ActionContext中存入的值,比如request的Map,如下  -->  
  16.         <s:property value="#request"/>  
  17.         <!-- 获得Struts2在ActionContext中存入的值,比如session的Map,如下  -->  
  18.         <s:property value="#session"/>  
  19.         <!-- 获得Struts2在ActionContext中存入的值,request请求传递的GET参数或POST参数的Map,如下  -->  
  20.         <s:property value="#parameters"/>  
  21.            
  22.         <!-- 以下演示在JSP中把值存入ActionContext中  -->  
  23.         <!-- 存入一个字符串"myName",key为"myKey",如下 -->  
  24.         <s:set value="%{'myName'}" var="myKey"/>  
  25.         <!-- 使用s:bean标签来创建一个对象,并把它存入ActionContext中,key为myObject,如下  -->  
  26.         <s:bean name="com.example.Person" var="myObject"/>  
  27.         <!-- 之后就可以用“#”来读取它们,如下  -->  
  28.         <s:property value="#myKey"/>  
  29.         <s:property value="#myObject"/>  
  30.     </body>  
  31. </html>  
[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5.         <title>JSP Page</title>  
  6.     </head>  
  7.     <body>  
  8.         <!-- 本JSP将演示在JSP中对ActionContext的使用 -->  
  9.         <!-- 本JSP为MyAction对应的JSP -->  
  10.   
  11.         <!-- 由于Action中已经向ActionContext存入了key为"person"的值,所以可以使用“#person”来获取它,如下  -->  
  12.         <s:property value="#person"/>  
  13.         <!-- 获得person的name属性,如下  -->  
  14.         <s:property value="#person.name"/>  
  15.         <!-- 获得Struts2在ActionContext中存入的值,比如request的Map,如下  -->  
  16.         <s:property value="#request"/>  
  17.         <!-- 获得Struts2在ActionContext中存入的值,比如session的Map,如下  -->  
  18.         <s:property value="#session"/>  
  19.         <!-- 获得Struts2在ActionContext中存入的值,request请求传递的GET参数或POST参数的Map,如下  -->  
  20.         <s:property value="#parameters"/>  
  21.           
  22.         <!-- 以下演示在JSP中把值存入ActionContext中  -->  
  23.         <!-- 存入一个字符串"myName",key为"myKey",如下 -->  
  24.         <s:set value="%{'myName'}" var="myKey"/>  
  25.         <!-- 使用s:bean标签来创建一个对象,并把它存入ActionContext中,key为myObject,如下  -->  
  26.         <s:bean name="com.example.Person" var="myObject"/>  
  27.         <!-- 之后就可以用“#”来读取它们,如下  -->  
  28.         <s:property value="#myKey"/>  
  29.         <s:property value="#myObject"/>  
  30.     </body>  
  31. </html>  
 

3. HttpServletRequest类或request的Map

    Struts2中提供了两种对request的操作:一种是Web服务器提供的HttpServletRequest类,这和传统Java Web项目中的操作request的方式相同;另一种是一个“request的Map”,即封装了HttpServletRequest的attributes的映射类,操作该Map相当于操作HttpServletRequest的attributes。之所以提供了Map的操作方式,一是方便操作,二是能方便使用Ognl在JSP标签中读取request。无论如何,这两个request是互通的。至于request的生命周期等概念,与其他的Java Web项目没有区别,本文不再详述。

 

    使用HttpServletRequest类还是request的Map
  • 虽然两者是互通的,但就读取request的attributes而言,使用request的Map要方便许多,并且不会暴露不必要的接口。当然,HttpServletRequest有一些request的Map没有的方法,使用这些方法时当然还是要用前者。
    使用request的Map还是ActionContext:
  • 两者都是Map,两者的生命周期都是一个请求。
  • 传统的Java Web项目中,往往是通过request的attributes来向JSP传递值的:先在Servlet里setAttribute(),然后在JSP里getAttribute()。当然在Struts2的项目中,你仍然可以使用这个方法,然而抛弃了Struts2提供的传递功能是得不偿失的。虽然笔者没有找到官方文档说一定要用ActionContext替换request的Map,也没有发现程序中有能获得ActionContext却获得不了request的Map的地方,但在Struts2框架下,操作ActionContext要比操作request的Map更加方便。因此,笔者建议:尽量使用ActionContext而不是request的Map来传递值。
  • request的Map有时候会包含其他框架设置的值,比如Spring框架。获取这些值的时候就需要用request的Map了,因为ActionContext里没有。
  • 通过ActionContext可以获得HttpServletRequest类:“HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);”。
  • 通过ActionContext也可以获得request的Map:“Map requestMap = (Map) actionContext.get("request");”。因此,在JSP标签中,使用表达式“#request”就可以获得request的Map的数据。
    如何获得HttpServletRequest:
  • 如果已经有ActionContext,则使用“actionContext.get(StrutsStatics.HTTP_REQUEST)”来获得HttpServletRequest。
  • 在自定义的拦截器中,先获得ActionContext,再通过ActionContext来获得。
  • 在Action中,先获得ActionContext,再通过ActionContext来获得。或者让Action实现ServletRequestAware接口,并使用ServletConfigInterceptor拦截器,这样这个拦截器就会注入HttpServletRequest。
  • 在JSP中,一般不需要获得HttpServletRequest。
    如何获得request的Map:
  • 如果已经有ActionContext,则使用“actionContext.get("request")”来获得。
  • 在自定义的拦截器中,先获得 ActionContext,再通过ActionContext来获得。
  • 在Action中,先获得ActionContext,再通过ActionContext来获得。或者让Action实现RequestAware接口,并使用ServletConfigInterceptor拦截器,这样这个拦截器就会注入Map request。
  • 在JSP中,用“#request”来获得request的Map,用“#request.key”或者“#request['key']”来读取Map中的值。

    总之,request仍然符合Java Web网站的一般规律。不过笔者建议使用者应尽量避免用request传值。

 

    一些例子:

 

Java代码 复制代码 收藏代码
  1. // 本类将演示拦截器中对HttpServletRequest和request的Map的操作   
  2. public class MyInterceptor extends AbstractInterceptor {   
  3.   
  4.     public String intercept(ActionInvocation invocation) throws Exception {   
  5.         // 获得ActionContext   
  6.         ActionContext actionContext = invocation.getInvocationContext();   
  7.         // 获得HttpServletRequest   
  8.         HttpServletRequest httpServletRequest=(HttpServletRequest)actionContext.get(StrutsStatics.HTTP_REQUEST);   
  9.         // 获得request的Map   
  10.         Map requestMap = (Map) actionContext.get("request");   
  11.         // 创建一个类作为实例   
  12.         Person person = new Person();   
  13.         // 以下两行的语句作用相同   
  14.         httpServletRequest.setAttribute("person", person);   
  15.         requestMap.put("person", person);   
  16.         // 其他代码   
  17.         // ......   
  18.         return invocation.invoke();   
  19.     }   
  20. }  
[java] view plain copy
  1. // 本类将演示拦截器中对HttpServletRequest和request的Map的操作  
  2. public class MyInterceptor extends AbstractInterceptor {  
  3.   
  4.     public String intercept(ActionInvocation invocation) throws Exception {  
  5.         // 获得ActionContext  
  6.         ActionContext actionContext = invocation.getInvocationContext();  
  7.         // 获得HttpServletRequest  
  8.         HttpServletRequest httpServletRequest=(HttpServletRequest)actionContext.get(StrutsStatics.HTTP_REQUEST);  
  9.         // 获得request的Map  
  10.         Map requestMap = (Map) actionContext.get("request");  
  11.         // 创建一个类作为实例  
  12.         Person person = new Person();  
  13.         // 以下两行的语句作用相同  
  14.         httpServletRequest.setAttribute("person", person);  
  15.         requestMap.put("person", person);  
  16.         // 其他代码  
  17.         // ......  
  18.         return invocation.invoke();  
  19.     }  
  20. }  

 

 

 

Java代码 复制代码 收藏代码
  1. // 本类将演示在Action中对HttpServletRequest和request的Map进行操作(静态方法获得ActionContext)  
  2. public class MyAction extends ActionSupport {   
  3.   
  4.     @Override  
  5.     public String execute() throws Exception {   
  6.         // 获得ActionContext   
  7.         ActionContext actionContext = ActionContext.getContext();   
  8.         // 获得HttpServletRequest   
  9.         HttpServletRequest httpServletRequest=(HttpServletRequest)actionContext.get(StrutsStatics.HTTP_REQUEST);   
  10.         // 获得request的Map   
  11.         Map requestMap = (Map) actionContext.get("request");   
  12.         // 创建一个类作为实例   
  13.         Person person = new Person();   
  14.         // 以下两行的语句作用相同   
  15.         httpServletRequest.setAttribute("person", person);   
  16.         requestMap.put("person", person);   
  17.         // 其他代码   
  18.         // ......   
  19.         return SUCCESS;   
  20.     }   
  21. }  
[java] view plain copy
  1. // 本类将演示在Action中对HttpServletRequest和request的Map进行操作(静态方法获得ActionContext)  
  2. public class MyAction extends ActionSupport {  
  3.   
  4.     @Override  
  5.     public String execute() throws Exception {  
  6.         // 获得ActionContext  
  7.         ActionContext actionContext = ActionContext.getContext();  
  8.         // 获得HttpServletRequest  
  9.         HttpServletRequest httpServletRequest=(HttpServletRequest)actionContext.get(StrutsStatics.HTTP_REQUEST);  
  10.         // 获得request的Map  
  11.         Map requestMap = (Map) actionContext.get("request");  
  12.         // 创建一个类作为实例  
  13.         Person person = new Person();  
  14.         // 以下两行的语句作用相同  
  15.         httpServletRequest.setAttribute("person", person);  
  16.         requestMap.put("person", person);  
  17.         // 其他代码  
  18.         // ......  
  19.         return SUCCESS;  
  20.     }  
  21. }  

 

 

 

Java代码 复制代码 收藏代码
  1. // 本类将演示在Action中使用ServletRequestAware获得HttpServletRequest(注意:要使用ServletConfigInterceptor拦截器)  
  2. public class MyAction extends ActionSupport implements ServletRequestAware {   
  3.   
  4.     private HttpServletRequest request;   
  5.        
  6.     //此方法是接口ServletRequestAware的方法   
  7.     public void setServletRequest(HttpServletRequest request) {   
  8.         this.request = request;   
  9.     }   
  10.   
  11.     @Override  
  12.     public String execute() throws Exception {   
  13.         // HttpServletRequest已在该类的字段中准备好,可直接使用  
  14.         // ......   
  15.         return SUCCESS;   
  16.     }   
  17. }  
[java] view plain copy
  1. // 本类将演示在Action中使用ServletRequestAware获得HttpServletRequest(注意:要使用ServletConfigInterceptor拦截器)  
  2. public class MyAction extends ActionSupport implements ServletRequestAware {  
  3.   
  4.     private HttpServletRequest request;  
  5.       
  6.     //此方法是接口ServletRequestAware的方法  
  7.     public void setServletRequest(HttpServletRequest request) {  
  8.         this.request = request;  
  9.     }  
  10.   
  11.     @Override  
  12.     public String execute() throws Exception {  
  13.         // HttpServletRequest已在该类的字段中准备好,可直接使用  
  14.         // ......  
  15.         return SUCCESS;  
  16.     }  
  17. }  

 

 

Java代码 复制代码 收藏代码
  1. // 本类将演示在Action中使用ServletRequestAware获得request的Map(注意:要使用ServletConfigInterceptor拦截器)  
  2. public class MyAction extends ActionSupport implements RequestAware {   
  3.   
  4.     Map<String, Object> request;   
  5.   
  6.     // 该方法是接口RequestAware的方法   
  7.     public void setRequest(Map<String, Object> request) {   
  8.         this.request = request;   
  9.     }   
  10.   
  11.     @Override  
  12.     public String execute() throws Exception {   
  13.         // request的Map已在该类的字段中准备好,可直接使用   
  14.         // ......   
  15.         return SUCCESS;   
  16.     }   
  17. }  
[java] view plain copy
  1. // 本类将演示在Action中使用ServletRequestAware获得request的Map(注意:要使用ServletConfigInterceptor拦截器)  
  2. public class MyAction extends ActionSupport implements RequestAware {  
  3.   
  4.     Map<String, Object> request;  
  5.   
  6.     // 该方法是接口RequestAware的方法  
  7.     public void setRequest(Map<String, Object> request) {  
  8.         this.request = request;  
  9.     }  
  10.   
  11.     @Override  
  12.     public String execute() throws Exception {  
  13.         // request的Map已在该类的字段中准备好,可直接使用  
  14.         // ......  
  15.         return SUCCESS;  
  16.     }  
  17. }  
 

 

Java代码 复制代码 收藏代码
  1. <!DOCTYPE html>   
  2. <html>   
  3.     <head>   
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  5.         <title>JSP Page</title>   
  6.     </head>   
  7.     <body>   
  8.         <!-- 本JSP将演示在JSP中对request的Map的使用 -->   
  9.         <!-- 本JSP为MyAction对应的JSP -->   
  10.   
  11.         <!-- request的Map是Struts2自动在ActionContext中存入的值(key为request),所以使用“#”来访问ActionContext,从中读取request  -->   
  12.         <s:property value="#request"/>   
  13.         <!-- 以下两行均是访问request的Map中key为“name”的值  -->   
  14.         <s:property value="#request.name"/>   
  15.         <s:property value="#request['name']"/>   
  16.     </body>   
  17. </html>  
[java] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5.         <title>JSP Page</title>  
  6.     </head>  
  7.     <body>  
  8.         <!-- 本JSP将演示在JSP中对request的Map的使用 -->  
  9.         <!-- 本JSP为MyAction对应的JSP -->  
  10.   
  11.         <!-- request的Map是Struts2自动在ActionContext中存入的值(key为request),所以使用“#”来访问ActionContext,从中读取request  -->  
  12.         <s:property value="#request"/>  
  13.         <!-- 以下两行均是访问request的Map中key为“name”的值  -->  
  14.         <s:property value="#request.name"/>  
  15.         <s:property value="#request['name']"/>  
  16.     </body>  
  17. </html>  

 

 

3. Parameters,即GET请求或POST请求的参数

    Parameters为GET或POST等请求时浏览器向服务器传递而来的参数。在传统的Java Web项目中,使用HttpServletRequest.getParameter()等方法来获取参数,并且可以直接使用HttpServletRequest.getParameterMap()来获得一个封装了参数的Map。而在Struts2中,Struts2直接把上述Map存放到了ActionContext中,key为“parameters”。另外,ActionContext还直接提供了ActionContext.getParameters()方法来获得这个Map。因此,在Struts2的各个部件中操作parameters的方法和操作request的Map的方法十分相似,本段不再详述。

 

4. HttpServletSession类和session的Map

    传统Java Web项目中的session是我们都熟悉的,我们用它来记录一个用户的会话状态。Struts2把HttpServletSession封装到了一个Map中,即“session的Map”,这类似对request的处理。然而为了节省系统资源,我们在不需要session的时候不会创建session。可能正是因为这个缘故,Struts2中没有把HttpServletSession放入ActionContext中,如果你的程序需要使用HttpServletSession,应该先获得HttpServletRequest,然后使用getSession()或getSession(boolean b)来获得它,同时决定是否需要创建session。对于session的Map,Struts2仍然把它放入了ActionContext中(key为"session"),但是不要担心,这个Map的机制使得只有put新值时才会创建session。总之,Struts2中对HttpServletSession的操作要先通过HttpServletRequest来获得它,而对session的Map的操作与对request的Map的操作如出一辙,本段不再详述。

 

5. ServletContext和application的Map

    传统的Java Web项目中,ServletContext用来存放全局变量,每个Java虚拟机每个Web项目只有一个ServletContext。这个ServletContext是由Web服务器创建的,来保证它的唯一性。ServletContext有一些方法能操作它的attributes,这些操作方法和操作一个Map类似。于是,Struts2又来封装了:它把ServletContext的attributes封装到了一个Map中,即“application的Map”,并且也放入的ActionContext中(key为application),因此,对application的Map的操作就如果对request的Map操作,本段不再详述。

    至于对ServletContext的操作,与HttpServletRequest的操作类似:Struts2将ServletContext放到了 ActionContext中,并且ServletConfigInterceptor提供了对ServletContext的注入接口ServletContextAware。因此,本段不再详述。

    注意:在Ognl表达式中使用“#application”可以得到application的Map,而不是ServletContext。然而在JSP嵌入的Java代码中(比如“<% application.getAttribute(""); %>”),application为ServletContext,而不是Map。

 

    用一张表格来总结:

 

 

变量从ActionContext中获得生命周期用Ongl来读取值使用ServletConfigInterceptor来注入ActionContext类静态方法ActionContext. getContext()一次Http请求使用“#”加上key,如“#name”无法注入ValueStack类ActionContext. getValueStack()一次Http请求直接填写来访问栈中对象的方法,或者使用top来直接获得栈中对象无法注入HttpServletRequest类ActionContext. get( StrutsStatics. HTTP_REQUEST)一次Http请求无方便的方法实现ServletRequestAware接口request的MapActionContext. get("request")一次Http请求使用“#request”再加上key,如“#request.name”或者“#request['name']”实现RequestAware接口parameters的MapActionContext. get( "parameters")一次Http请求使用“# parameters”再加上key,如“#parameters .name”或者“#parameters ['name']”实现ParameterAware接口HttpServletSession类无(需通过HttpServletRequest来获得)一次Http Session会话无方便的方法无法注入session的MapActionContext. get("session")每次请求创建,但在一次Http Session会话中数据都是一样的使用“#session”再加上key,如“# session.name”或者“#session ['name']”实现SessionAware接口ServletContext类ActionContext. get( StrutsStatics. SERVLET_CONTEXT)网站项目启动后一直存在且唯一无方便的方法使用ServletContextAware接口application的MapActionContext.get( "application")每次请求时创建,但其中的数据是网站项目启动后一直存在且共享使用“# application”再加上key,如“#application .name”或者“#application ['name']”使用ApplicationAware接口

 

附录1 ActionContext中到底有哪些数据

 

keykey的声明处value的类型value.toString()

com. opensymphony. xwork2. dispatcher.

HttpServletRequest

 StrutsStatics. HTTP_REQUESTorg. apache. struts2. dispatcher. StrutsRequestWrapperorg. apache. struts2. dispatcher. StrutsRequestWrapper @10984e0application 无org. apache. struts2. dispatcher. ApplicationMap略com. opensymphony. xwork2. ActionContext. locale ActionContext. LOCALEjava. util. Localezh_CNcom. opensymphony. xwork2. dispatcher. HttpServletResponse StrutsStatics. HTTP_RESPONSEorg. apache. catalina. connector. ResponseFacadeorg. apache. catalina. connector. ResponseFacade @14ecfe8

xwork. NullHandler.

createNullObjects

 Booleanfalsecom. opensymphony. xwork2. ActionContext. name ActionContext. ACTION_NAMEStringindex

com.opensymphony. xwork2.ActionContext.

conversionErrors

 ActionContext. 

CONVERSION_ERRORS

java. util. HashMap{}com. opensymphony. xwork2. ActionContext. application ActionContext. APPLICATIONorg. apache. struts2. dispatcher. ApplicationMap略attr 无org. apache. struts2. util. AttributeMaporg. apache. struts2. util. AttributeMap @133a2a8com. opensymphony. xwork2. ActionContext. container ActionContext. CONTAINERcom. opensymphony. xwork2. inject. ContainerImplcom. opensymphony. xwork2. inject. ContainerImpl @fc02c8com. opensymphony. xwork2. dispatcher. ServletContext StrutsStatics. SERVLET_CONTEXTorg. apache. catalina. core. ApplicationContextFacadeorg. apache. catalina. core. ApplicationContextFacade @11ad78ccom. opensymphony. xwork2. ActionContext. session ActionContext. SESSIONorg.apache.struts2. dispatcher.SessionMap{}

com.opensymphony. xwork2.ActionContext.

actionInvocation

 ActionContext. ACTION_INVOCATIONcom. opensymphony. xwork2. DefaultActionInvocationcom. opensymphony. xwork2. DefaultActionInvocation @13d4497xwork. MethodAccessor. denyMethodExecution 笔者很懒,没有找Booleanfalsereport. conversion. errors 笔者很懒,没有找Booleanfalsesession 无org. apache. struts2. dispatcher. SessionMap{}com. opensymphony. xwork2. util. ValueStack. ValueStack ValueStack.VALUE_STACKcom. opensymphony. xwork2. ognl. OgnlValueStackcom. opensymphony. xwork2. ognl. OgnlValueStack @16237fdrequest 无org. apache. struts2. dispatcher. RequestMap略action 笔者很懒,没有找com. example. MyAction略struts. actionMapping 笔者很懒,没有找org. apache. struts2. dispatcher. mapper. ActionMappingorg. apache. struts2. dispatcher. mapper. ActionMapping @892cc5parameters 无java. util. HashMap{}com. opensymphony. xwork2. ActionContext. parameters ActionContext.PARAMETERSjava. util. TreeMap{}

 

注意:该表格为了排版在某些地方加了空格。

 

可以看出,有些相同的对象被以不同的key多次设置到ActionContext中。如果想看看创建ActionContext的源代码,请看org.apache.struts2.dispatcher.Dispatcher的serviceAction方法和两个createContextMap方法。


附录2 Struts2标签中value属性直接对ActionContext访问的问题

    经试验并查看相关源代码后发现,在使用<s:property value="..."/>时,该标签的执行类会先根据value中表达式到值栈中执行表达式来查找值。如果在值栈中找到值,就返回该值;如果没有找到,则把该表达式作为ActionContext的key,到ActionContext中去找值。比如<s:property value="request"/>也会得到ActionContext中的request,等价于<s:property value="#request"/>。但是,由于标签的执行类会认为该值时String类型的,并且会直接进行类型转换。于是,如果直接使用<s:property value="request"/>的话其实会让页面抛出异常:Request类型不能转换成String类型。所以,只能用如果不带#的话只能成功读取ActionContext中String类型的值。这种机制使得某些时候栈顶的属性可以覆盖ActionContext中的key,或许你正需要它。然而,鉴于这种机制的不确定性,笔者建议访问ActionContext中的数据一定要带上“#”,可以免去一些麻烦。

    关于这种转型异常,笔者认为是Struts2的bug,源代码如下,当“value = getValue(expr, asType);”时,是考虑了asType的,但从context中读取时“value = findInContext(expr);”,就没有考虑asType,并且没有在其他地方看到类型检查操作:

 

 

Java代码 复制代码 收藏代码
  1. // 本代码截取Struts2.3.1.2版本com.opensymphony.xwork2.ognl.OgnlValueStack类的第340行-352行  
  2.     private Object tryFindValue(String expr, Class asType) throws OgnlException {   
  3.         Object value = null;   
  4.         try {   
  5.             expr = lookupForOverrides(expr);   
  6.             value = getValue(expr, asType);   
  7.             if (value == null) {   
  8.                 value = findInContext(expr);   
  9.             }   
  10.         } finally {   
  11.             context.remove(THROW_EXCEPTION_ON_FAILURE);   
  12.         }   
  13.         return value;   
  14.     }  
[java] view plain copy
  1. // 本代码截取Struts2.3.1.2版本com.opensymphony.xwork2.ognl.OgnlValueStack类的第340行-352行  
  2.     private Object tryFindValue(String expr, Class asType) throws OgnlException {  
  3.         Object value = null;  
  4.         try {  
  5.             expr = lookupForOverrides(expr);  
  6.             value = getValue(expr, asType);  
  7.             if (value == null) {  
  8.                 value = findInContext(expr);  
  9.             }  
  10.         } finally {  
  11.             context.remove(THROW_EXCEPTION_ON_FAILURE);  
  12.         }  
  13.         return value;  
  14.     }  
1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 车保险贴掉了怎么办 小车撞凹进去了怎么办 被代位追偿了怎么办 车被别人抵押了怎么办 朋友没驾照借车怎么办 车子被朋友撞了怎么办 电车被交警扣了怎么办 e照扣了12分怎么办 驾驶证被扣33分怎么办 a2驾驶员扣33分怎么办 驾驶证被扣48分怎么办 驾照扣了33分怎么办 车辆被扣36分怎么办 车被朋友借走了怎么办 在中国终身禁驾怎么办 c驾驶证忘年审了怎么办 驾驶证脱审1年多怎么办 驾驶证脱审四个月怎么办 驾照b2过期没审怎么办 a照驾照过期没审怎么办 驾照扣60多分怎么办 无证违章被扣分怎么办 变味的牛奶喝了怎么办 孩子喝了坏牛奶怎么办 孕妇喝了坏牛奶怎么办 驾照过期五年了怎么办 科目一没过之后怎么办? 重庆科目一没过怎么办 科三过了没签字怎么办 驾照过期几天撞死人怎么办 办理过期身份时出错了怎么办 新加坡半年临时驾照过期后怎么办 北京怎么办残摩行驶证 报考驾照三年到期了怎么办 车辆违章扣3分怎么办 驾照过期忘审了怎么办 外省港澳证办了居住证怎么办 电动车交警罚单丢了怎么办 在外地开车违章扣分怎么办 郑州车在外地扣分违章怎么办 电工证掉了应该怎么办