struts2 request内幕 为什么在struts2用EL表达式可以取值
不知道大家有没有想过这样一个问题:为什么在action中的实例变量,没有使用request.setAttribute()方法将值添加到request范围内,却能在jsp中用EL表达式取出?
众所周知,EL表达式只能取出pageContext,request,session,application属性范围的值。然而,在struts2中能突破这一个限制,成功的取出action中的实例变量值。
请看例子:
这是一个action
package com.wuyou.action; import com.opensymphony.xwork2.ActionSupport;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; @Controller
@Scope(value = "prototype")
public class TestAction extends ActionSupport { private Integer id = 123;
private String name = "无忧之路"; public String execute() {
return "success";
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
对应的struts配置文件:
<package name="default" extends="struts-default" namespace="/">
<action name="test" class="testAction">
<result>/success.jsp</result>
</action>
</package>
对应的success.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body> ${id} </body>
</html>
页面输出:123
其实,在struts2中使用的request并非为tomcat提供的,而是经过了struts2所包装过的org.apache.struts2.dispatcher.StrutsRequestWrapper对象。
这个类做了些什么事情呢?
原来,我们在调用EL表达式的时候,或者request.getAttribute(String key)方法的时候,struts2会先在原来的request中调用request.getAttribute()方法获取该值,如果查找不到,则继续往OgnlValueStack查找,由于action对象在ognl值栈,返回action里名为"id"的实例变量值,即可显示在页面上。
我们可以尝试在jsp中输出request的类名:
<%=request.getClass()%>
也可以在action里面输出:
System.out.println(ServletActionContext.getRequest().getClass());
均输出:class org.apache.struts2.dispatcher.StrutsRequestWrapper
这下真相大白了吧!
附StrutsRequestWrapper类源代码:(点击展开)
public class StrutsRequestWrapper extends HttpServletRequestWrapper {
/**
* The constructor
* @param req The request
*/
public StrutsRequestWrapper(HttpServletRequest req) {
super(req);
}
/**
* Gets the object, looking in the value stack if not found
*
* @param s The attribute key
*/
public Object getAttribute(String s) {
if (s != null && s.startsWith("javax.servlet")) {
// don't bother with the standard javax.servlet attributes, we can short-circuit this
// see WW-953 and the forums post linked in that issue for more info
return super.getAttribute(s);
}
ActionContext ctx = ActionContext.getContext();
Object attribute = super.getAttribute(s);
if (ctx != null) {
if (attribute == null) {
boolean alreadyIn = false;
Boolean b = (Boolean) ctx.get("__requestWrapper.getAttribute");
if (b != null) {
alreadyIn = b.booleanValue();
}
// note: we don't let # come through or else a request for
// #attr.foo or #request.foo could cause an endless loop
if (!alreadyIn && s.indexOf("#") == -1) {
try {
// If not found, then try the ValueStack
ctx.put("__requestWrapper.getAttribute", Boolean.TRUE);
ValueStack stack = ctx.getValueStack();
if (stack != null) {
attribute = stack.findValue(s);
}
} finally {
ctx.put("__requestWrapper.getAttribute", Boolean.FALSE);
}
}
}
}
return attribute;
}
}
struts2 request内幕 为什么在struts2用EL表达式可以取值的更多相关文章
- java实体类的属性名首字母不能大写,不然el表达式无法取值
摘要:Java命名规范中,实体类属性名以小写字母开头,但并没有说不能以大写字母开头,然而事实告诉我,大写真不行 https://www.cnblogs.com/jnhs/p/10025757.html
- struts2的@Result annotation 如何添加params,并且在页面取值
http://www.bubuko.com/infodetail-2492575.html .............................................. 标签:lai ...
- Struts2技术内幕----深入解析Struts2架构与设计(一)
Struts2的核心入口程序,从功能上来说必须能够处理Http请求,这是表示层框架的基本要求.为了达到这一目的,Struts2毫无例外地遵循了Servlet标准,通过实现标准的Filter接口来进行H ...
- EL表达式详解(常用表达式以及取值)
EL表达式 学习总结 一. El表达式概念 二. El中的表达式 1. 算术表达式 2. 比较表达式 3. 逻辑表达式 4. 三元表达式 5. 判空表达式 三.EL 从四个作用域中取值 1. 概念 2 ...
- EL表达式获取属性值的原理
EL表达式获取对象属性的原理是这样的:以表达式${user.name}为例EL表达式会根据name去User类里寻找这个name的get方法,此时会自动把name首字母大写并加上get前缀,一旦找到与 ...
- 如何将数据库中的值经过servlet传入到jsp页面,并且用EL表达式显示出值
方法一:通过id查询某一数据库表中具体的行,将值封装在相应的对象中,如下面的对象Notice servlet中 String noticeId=request.getParameter("n ...
- jsp使用EL表达式回传boolean值出错的问题
在最近做的一个项目中使用session回传的属性中有一个为boolean,报出错. 属性名字为"isAdmit",布尔类型.后来我上网查了一下,是因为我使用了Myeclipse的自 ...
- HTML5中的时间类型,另外EL表达式的时间值来读取时间,并且还可以更改时间
HTML5规范里只规定date新型input输入类型,并没有规定日历弹出框的实现和样式.所以,各浏览器根据自己的设计实现日历.目前只有谷歌浏览器完全实现日历功能.相信这种局面很快就会结束,所有的浏览器 ...
- 简述jsp之EL表达式和jstl及其使用
Jsp的指令之include指令include指令:代表的是页面的包含. 作用:可以把一些jsp的页面包含在一起,对外展示. 页面的布局,现在已经不用了,现在都用css+div进行布局.include ...
随机推荐
- Setup Project 安装项目
从vs2012起,微软已经不支持setup project了.以此纪念一下setup project. 在新建Setup Project 增加安装内容,通常是直接Oupput一个项目,或者直接 ...
- 使用gulp脚本配合TypeScript开发
目标:编写TypeScript时,保存即生成js文件. 使用npm安装以下组件 gulp gulp-rename through-gulp gulp-typescript 编写gulpfile ...
- js画了一个椭圆
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [C#]线程处理
线程处理用于使程序能够执行并发处理,同时执行多个操作.C#中有三种线程的使用方法,BackgroundWorker组件.线程池.自己创建使用线程,接下来分别介绍如何使用. 1.使用Background ...
- Python快速入门学习笔记(三)——函数的定义与调用
定义函数 Python中定义函数的格式为: def fun([参数列表]): ... 函数在执行到return语句时结束,并将结果返回.如果没有return语句,函数执行完毕后返回None. 例: d ...
- UVaLive6834 Shopping
题意:一条直线上有n个点,标号从1到n,起点为0,终点为N+1,给定m个限制关系(ci,di),访问ci点前必须先访问di点,每两个点之间是单位距离,求在限制条件下,从起点到终点访问完所有的点的最短距 ...
- 阿里云 centos 环境配置与 django 部署
1. 免密码登陆 # 本机生成密钥, 并将 pub 复制到阿里云服务器上 $ ssh-keygen -t rsa -P '' # -P表示密码,-P '' 就表示空密码 $ scp ~/.ssh/FI ...
- c++ algorithm 的用法
1 , accumulate()template<class _II, class _Ty> inline_Ty accumulate(_II _F, _II _L, _Ty _V){fo ...
- Poj 2109 / OpenJudge 2109 Power of Cryptography
1.Link: http://poj.org/problem?id=2109 http://bailian.openjudge.cn/practice/2109/ 2.Content: Power o ...
- mysql---用户管理
#创建用户king , 登陆密码为1234 create user 'king' identified by '1234'; #查看创建用户的语句,即上面那条创建用户的语句 show grants f ...