struts2中actionContext().put()方法到底把东西放哪了
来源:互联网 发布:淘宝店扣分有什么影响 编辑:程序博客网 时间:2024/05/08 03:42
在一个action中,有一个属性test,
- private String test;
execute方法如下:
- public String execute() throws Exception {
- test = "vs_test";
- ActionContext.getContext().put("test", "actionContext_test");
- return "success";
- }
在页面中使用下面两种方法输出:
- ${requestScope.test}
- <%= ActionContext.getContext().get("test") %>
输出结果分别为:
- vs_test
- actionContext_test
如果把execute()方法中
- test = "vs_test";
注释掉;
输出结果为:
- actionContext_test
- actionContext_test
action中赋值都没有使用 request.setAttribute()进行赋值,
根据资料 ${requestScope.test} 会先通过 request.getAttribute()取值,如果取不到则从 valueStack 中去,
根据第一次的输出结果,可以看出 test 在 valueStack 中的值为 vs_test,通过 <s:debug/> 也可以看出,
根据第二次的输出结果,可以看出 ActionContext.getContext().put("test", "actionContext_test") 也是把值放到 valueStack 中了,
但为什么第一次两种输出方式的输出值不一样哪?
另外如果我在execute()方法中放入以下代码:
- ActionContext.getContext().getValueStack().set("test", "hello");
${requestScope.test }的 输出结果变成了 hello,
此时在<s:debug/>中 可以看到作为当前action对象属性的test ,其值仍为 vs_test,但是没有我手动放进去的 test,
那我通过上面代码放进去的值为 hello 的 test去哪里了?
各位大大,求指导!!
actionContext.getContext().put()就是把属性放到action上下文中,这个actionContext本身 就是一个Map,里面放有默认的Map session等常用属性,比如可以actionContext.getContext().get(StrutsStatics.HTTP_REQUEST)得到struts2为我们action保存的HttpServletRequest对象 ,而你Put一个put("test", "actionContext_test"),放的就是一个String对象 。而el表达式能取到action的属性,是由于struts拦截了request.getAttribute()方法并重新实现了它,如果HttpServletRequest没有我们要的属性,就从actionContext中找我们Put的对象 ,再没有就找valuestack中找,所有你后面把自定义 的test放到值栈中去了,就把本身action中的test属性盖了,el就只能找到这个hello了。
可以参考 下面:我们知道,JSTL默认是从page,request,session,application这四个Scope逐次查找相应的EL表达式所对应的对象的值。那么如果要使用JSTL来读取Action中的变量,就需要把Action中的变量,放到request域中才行。所以,早在 Webwork2.1.X的年代,我们会编写一个拦截器来做这个事情的。大致的原理是:在Action执行完返回之前,依次读取Action中的所有的变量,并依次调用request.setAttribute()来进行设置。具体的整合方式,请参考以下这篇文档:http://wiki.opensymphony.com/display/WW/Using+WebWork+and+XWork+with+JSP+2.0+and+JSTL+1.1
不过随着时代的发展,上面的这种方式,已经不再被推荐使用了。(虽然如此,我们依然可以学习它的一个解决问题的思路)目前来说,自从Webwork2.2以后,包括Struts2,都使用另外一种整合方式:对HttpServletRequest进行装饰。让我们来看一下源码:
Java代码
1. public class StrutsRequestWrapper extends HttpServletRequestWrapper {
2.
3.
7. public StrutsRequestWrapper(HttpServletRequest req) {
8. super(req);
9. }
10.
11.
16. public Object getAttribute(String s) {
17. if (s != null && s.startsWith("javax.servlet")) {
18. // don't bother with the standard javax.servlet attributes, we can short-circuit this
19. // see WW-953 and the forums post linked in that issue for more info
20. return super.getAttribute(s);
21. }
22.
23. ActionContext ctx = ActionContext.getContext();
24. Object attribute = super.getAttribute(s);
25.
26. boolean alreadyIn = false;
27. Boolean b = (Boolean) ctx.get("__requestWrapper.getAttribute");
28. if (b != null) {
29. alreadyIn = b.booleanValue();
30. }
31.
32. // note: we don't let # come through or else a request for
33. // #attr.foo or #request.foo could cause an endless loop
34. if (!alreadyIn && attribute == null && s.indexOf("#") == -1) {
35. try {
36. // If not found, then try the ValueStack
37. ctx.put("__requestWrapper.getAttribute", Boolean.TRUE);
38. ValueStack stack = ctx.getValueStack();
39. if (stack != null) {
40. attribute = stack.findValue(s);
41. }
42. } finally {
43. ctx.put("__requestWrapper.getAttribute", Boolean.FALSE);
44. }
45. }
46. return attribute;
47. }
}
- struts2中actionContext().put到底把东西放哪了
- struts2中actionContext().put()方法到底把东西放哪了
- struts2中关于ActionContext.put()方法
- struts2中ActionContext是什么东西?
- Struts2里ActionContext的put方法和request的setAttribute方法
- Struts2——ActionContext的put方法和request的setAttribute方法
- struts2 中ActionContext.getContext().getSession().put("","")是什么意思 和HttpSession有什么区别吗?
- Struts2中ActionContext介绍
- Struts2中ActionContext
- Struts2中ActionContext介绍
- Struts2中ActionContext介绍
- struts2中ActionContext应用
- struts2中ActionContext是什么
- Struts2使用中用ActionContext.getContext().put()报错
- struts2中ActionContext是什么东东???
- struts2中ActionContext是什么东东???
- Struts2中ActionContext、 ServletActionContext详解
- 到底应该把网站放在哪里?
- VB,打开文件时,出现名称冲突
- Java日期转换SimpleDateFormat格式大全
- 【读书笔记】大数据 数据挖掘 相关
- VC6.0使用GDI+的方法及初始化工作
- 将NavigationBar设置为透明
- struts2中actionContext().put()方法到底把东西放哪了
- 2013 ACM/ICPC Asia Regional Online (吉林省赛)
- 最近比较烦
- 错误总结:error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
- eoe开发者大会
- ios7新特性
- aof_AOF品牌_AOF服饰_AOF联系方式
- java 实现视频转换通用工具类:视频相互转换-总方法及Mencoder(二)
- Android广播(内部类)使用