Struct2中context.put()与request.setAttribute()

在Servlet和jsp的交互中,使用ActionContext.getContext.put(,)方法把值放到了哪里,使用request.setAttribute(,)把值又放到了哪里。StackContext和ValueStack,request都有什么区别。对StackContext,ValueStack,request不是很清楚的小伙伴可以看看~一、StackContext,ValueStack和request

先看结论吧:

action的实例保存在ValueStack中。

ActionContext.getContext.put(String,Object)是把对象放到了StackContext中,这个对象跟request,session等一样,它们平起平坐,但这些都不是根对象,所以要通过#访问。

request.setAttribute(String,Object)就是把值放到request范围,而StackConext里含有request对象,所以可以通过#request.*来访问。

然后看以下Action的代码

public class TestAction extends ActionSupport implements ServletRequestAware{

  1.         private String tip;
  2.         private HttpServletRequest request;
  3.         public String execute()throws Exception{
  4.                 setTip(“tip来源ValueStack”);
  5.                 ActionContext.getContext().put(“tip”“tip来源StackContext”);
  6.                 request.setAttribute(“tip”“tip来源Request”);
  7.                 return SUCCESS;
  8.         }
  9.         @Override
  10.         public void setServletRequest(HttpServletRequest request) {
  11.                 this.request = request;
  12.         }
  13.         public String getTip() {
  14.                 return tip;
  15.         }
  16.         public void setTip(String tip) {
  17.                 this.tip = tip;
  18.         }
  19. }

分别往ValueStack,StackContext和request这3个范围里放入了key一样,值不一样的数据。

然后在jsp中访问3个范围的tip:

  1. <body>
  2.         <s:debug/>
  3.         <table>
  4.                 <tr>
  5.                         <td>访问ValueStack</td>
  6.                         <td><s:property value=“tip”/></td>
  7.                 </tr>
  8.                 <tr>
  9.                         <td>访问StackContext</td>
  10.                         <td><s:property value=“#tip”/></td>
  11.                 </tr>
  12.                 <tr>
  13.                         <td>访问Request</td>
  14.                         <td><s:property value=“#request.tip”/></td>
  15.                 </tr>
  16.         </table>
  17. </body>

第一行访问ValueStack里的tip

第二行访问StackContext里的tip,相当于ActionContext.getContext.get(“tip”);

第三行访问request范围的tip,相当于request.getAttribute(“tip”);

结果:

访问ValueStack tip来源ValueStack
访问StackContext tip来源StackContext
访问Request tip来源Request

在Debug中也确实可以找到ValueStack,StackContext,request里分别对应的tip,就是开头的结论。

ValueStack可以看做StackContext的根对象,那接下来讨论一下StackContext和Request。

二、request获取StackContext里的值

request通过request.getAttribute()是否可以获取到StackContext里的数据呢。

答案是可以。

把刚才action里的request.setAttribute(“tip”, “tip来源Request”);这句代码注释掉,也就是说不往request范围放数据。其他代码不变

然后执行结果为:

访问ValueStack tip来源ValueStack
访问StackContext tip来源StackContext
访问Request tip来源ValueStack

 

在request范围没有值时,获取到了ValueStack的数据。那把ValueStack里的数据也删除掉看看结果。

注释掉tip属性和方法,执行结果为:

访问ValueStack tip来源StackContext
访问StackContext tip来源StackContext
访问Request tip来源StackContext

 

所以就能看出结果了。

当调用request.getAttribute()时,首先会从request范围的数据里找,然后从ValueStack里找,最后到StackContext里找。

而使用<s:property value=”tip”/>时,ValueStack里没有,会继续往StackContext里找,而不是为null

三、结论

StackContext含有request对象。执行request的getAttribute时,首先会从曾经request范围的数据里找,然后从ValueStack里找,最后到StackContext里找。