不知道大家有没有想过这样一个问题:为什么在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表达式可以取值的更多相关文章

  1. java实体类的属性名首字母不能大写,不然el表达式无法取值

    摘要:Java命名规范中,实体类属性名以小写字母开头,但并没有说不能以大写字母开头,然而事实告诉我,大写真不行 https://www.cnblogs.com/jnhs/p/10025757.html

  2. struts2的@Result annotation 如何添加params,并且在页面取值

    http://www.bubuko.com/infodetail-2492575.html .............................................. 标签:lai  ...

  3. Struts2技术内幕----深入解析Struts2架构与设计(一)

    Struts2的核心入口程序,从功能上来说必须能够处理Http请求,这是表示层框架的基本要求.为了达到这一目的,Struts2毫无例外地遵循了Servlet标准,通过实现标准的Filter接口来进行H ...

  4. EL表达式详解(常用表达式以及取值)

    EL表达式 学习总结 一. El表达式概念 二. El中的表达式 1. 算术表达式 2. 比较表达式 3. 逻辑表达式 4. 三元表达式 5. 判空表达式 三.EL 从四个作用域中取值 1. 概念 2 ...

  5. EL表达式获取属性值的原理

    EL表达式获取对象属性的原理是这样的:以表达式${user.name}为例EL表达式会根据name去User类里寻找这个name的get方法,此时会自动把name首字母大写并加上get前缀,一旦找到与 ...

  6. 如何将数据库中的值经过servlet传入到jsp页面,并且用EL表达式显示出值

    方法一:通过id查询某一数据库表中具体的行,将值封装在相应的对象中,如下面的对象Notice servlet中 String noticeId=request.getParameter("n ...

  7. jsp使用EL表达式回传boolean值出错的问题

    在最近做的一个项目中使用session回传的属性中有一个为boolean,报出错. 属性名字为"isAdmit",布尔类型.后来我上网查了一下,是因为我使用了Myeclipse的自 ...

  8. HTML5中的时间类型,另外EL表达式的时间值来读取时间,并且还可以更改时间

    HTML5规范里只规定date新型input输入类型,并没有规定日历弹出框的实现和样式.所以,各浏览器根据自己的设计实现日历.目前只有谷歌浏览器完全实现日历功能.相信这种局面很快就会结束,所有的浏览器 ...

  9. 简述jsp之EL表达式和jstl及其使用

    Jsp的指令之include指令include指令:代表的是页面的包含. 作用:可以把一些jsp的页面包含在一起,对外展示. 页面的布局,现在已经不用了,现在都用css+div进行布局.include ...

随机推荐

  1. RSS订阅推荐

    科技新闻 虎嗅网 http://www.huxiu.com/ 科技博客的新生力量,文章以观点鲜明出名: 36氪  http://www.36kr.com/ 科技博客,关注创业,可以免费发表创业公司新闻 ...

  2. java新手笔记31 集合实现类

    Person类: package com.yfs.javase; import java.util.Date; public class Person implements Comparable { ...

  3. 引用类型之Function类型

    Function类型 ECMAScript中最有意思的就是函数了,有意思的根源,在于函数实际上是对象.每个函数都是Function的实例,具有属性和方法.而重要的一点是,函数名,不过是指向函数的指针, ...

  4. Php的安装与配置

    PHP的安装 php不需要安装,只是一个软件包,在Apache启动的过程中加载即可 PHP的配置 php是一个软件包,只需要在apache启动过程中加载即可,Php对于apache是一个功能模块. 测 ...

  5. 你早该这么玩Excel 读书笔记

    1. Excel用来分析数据,至少要有一份原始数据和对于的分类汇总数据,这两种数据在一项任务中,应该是存放在同一个Excel文档中的,在书籍中,把他们叫做源数据表和分类汇总表.用户输入源数据表,根据相 ...

  6. windows下redis 开机自启动

    1,在redis的目录下执行(执行后就作为windows服务了) redis-server --service-install redis.windows.conf 2,安装好后需要手动启动redis ...

  7. 【C#高级编程(学习与理解)】1.1 C#与.NET的关系

    1.C#语言不能孤立使用,而必须和.NET Framework一起考虑.C#编译器专门用于.NET,这表示用C#编写的所有代码总是在.NET Framework中运行. 2.C#就其本身而言只是一种语 ...

  8. Brackets - 又一款牛x的WEB开发编辑器

    Brackets官网下载: http://brackets.io/ Adobe Brackets是由Adobe主导开发一款主打web开发的编辑器. 是继TextMate,Sublime Text这两个 ...

  9. sql2005下载和安装

    下载地址:个人百度网盘 http://pan.baidu.com/s/1kTvKIZd sql05安装包.rar 1.54G这个 安装方法: 先安装Tool 在安装Server http://bbs. ...

  10. htmlcleaner

    String xpath = "//div"; Object[] myNodes = node.evaluateXPath(xpath); for (Object obj : my ...