Struts2的ValueStack及EL表达式中取ValueStack的内幕
来源:互联网 发布:软件人才缺口多少 编辑:程序博客网 时间:2024/05/22 07:41
1. Struts2的ValueStack是什么:
参见李腾飞老师的讲解:http://blog.csdn.net/li_tengfei/article/details/6098134
2. Struts2的JSP为何可以通过EL表达式取得Action实例变量里的值(本质是ValueStack中的值):
参见:http://www.tuicool.com/articles/bQzaEzI
补充一点:
现在有以下情况:
public class LoginAction extends ActionSupport {private String username;private String password;@Overridepublic String execute() throws Exception {<span style="color:#FF0000;">setUsername("New UserName");</span>setPassword("New Password");<span style="color:#FF0000;">ActionContext.getContext().put("username", "Shit");</span>}那么JSP用EL表达式${requestScope.username}将得到什么值呢?
通过调试org.apache.struts2.dispatcher.StrutsRequestWrapper的getAttribute(String key)方法,发现它会尝试从ValueStack中取值,而且是调用com.opensymphony.xwork2.ognl.OgnlValueStack的tryFindValue(String expr)方法取的:
对Action中的username分别单独采用setUsername()和ActionContext.getContext.put()来调试,发现在com.opensymphony.xwork2.ognl.OgnlValueStack的tryFindValue(String expr)方法中,setUsername()是调用getValueUsingOgnl(expr)取值, 而ActionContext.getContext.put()是调用findInContext(expr)取值的。
至此,真相大白了。
原来,Action中的set方法是对应到ValueStack的根元素,而ActionContext.getContext.put()方法则是把值设到ValueStack的context元素中,由于context是非根元素,所以我们在JSP中用${requestScope.username}来导航对象,其实就是用先尝试从ValueStack的根对象找username,找不到再从ValueStack的context对象找。因此${requestScope.username}显示的还是“New UserName".
以下的测试再次证明上述两种设置方式在ValueStack里的区别:
- Struts2的ValueStack及EL表达式中取ValueStack的内幕
- Struts2中ValueStack的学习
- struts2中几个核心的组件ActionContext、ValueStack、OGNL表达式
- struts2中几个核心的组件ActionContext、ValueStack、OGNL表达式
- el表达式怎样取关键字带有点的属性的值,比如key="struts.valueStack"
- Struts2中OGNL,valueStack,stackContext的学习
- Struts2中OGNL,valueStack,stackContext的学习
- Struts2中OGNL,valueStack,stackContext的学习
- Struts2中OGNL,valueStack,stackContext的学习
- Struts2中OGNL,valueStack,stackContext的学习
- Struts2中OGNL,valueStack,stackContext的学习
- Struts2中OGNL,valueStack,stackContext的学习
- struts2框架中关于ValueStack的认识
- Struts2中OGNL,valueStack,stackContext的学习
- Struts2中OGNL,valueStack,stackContext的学习
- 为何使用EL了表达式能够访问valueStack中对象的属性-源码分析
- Struts2的valueStack与actionContext(contextMap)的取值
- 关于struts2.0的valuestack
- Python学习笔记-生成器
- JavaScript/jQuery WebIM 及时聊天通信工具 本地客户端
- PendingIntent
- 2012年5月SAT香港真题解析
- UINavigationController与UITabbarController的样式
- Struts2的ValueStack及EL表达式中取ValueStack的内幕
- Android 代码混淆
- 优化之误!
- 联合(union)用法
- Application.mk文件详解
- [转]JAVA学习路线及推荐书籍
- [并查集]poj2236 Wireless Network
- Android屏幕适配
- poj 1088 滑雪