1、问题

提交页面:

	<h4>注册产品</h4>
<form action="save.do" method="post">
pName:<input type="text" name="pName" /><br />
pDesc:<input type="text" name="pDesc" /><br />
pPrice:<input type="text" name="pPrice" /><br />
<input type="submit" value="submit" />
</form>

响应页面:

	<h4>详细信息</h4>
Name:${pName}<br/><br/>
Desc:${pDesc}<br/><br/>
Price:${pPrice}<br/><br/>

${pName},${pDesc},${pPrice},写法简单优雅,优雅的背后肯定有故事,struts2在背后帮我们做了些什么?

2、StrutsRequestWrapper

(1)打印出request看看

request:<%=request %>
request:org.apache.struts2.dispatcher.StrutsRequestWrapper@37c4d046

(2)StrutsRequestWrapper源码

package org.apache.struts2.dispatcher;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper; import static org.apache.commons.lang3.BooleanUtils.isTrue; public class StrutsRequestWrapper extends HttpServletRequestWrapper { private static final String REQUEST_WRAPPER_GET_ATTRIBUTE = "__requestWrapper.getAttribute";
private final boolean disableRequestAttributeValueStackLookup; /**
* The constructor
* @param req The request
*/
public StrutsRequestWrapper(HttpServletRequest req) {
this(req, false);
} /**
* The constructor
* @param req The request
* @param disableRequestAttributeValueStackLookup flag for disabling request attribute value stack lookup (JSTL accessibility)
*/
public StrutsRequestWrapper(HttpServletRequest req, boolean disableRequestAttributeValueStackLookup) {
super(req);
this.disableRequestAttributeValueStackLookup = disableRequestAttributeValueStackLookup;
} /**
* Gets the object, looking in the value stack if not found
*
* @param key The attribute key
*/
public Object getAttribute(String key) {
if (key == null) {
throw new NullPointerException("You must specify a key value");
} if (disableRequestAttributeValueStackLookup || key.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(key);
} ActionContext ctx = ActionContext.getContext();
Object attribute = super.getAttribute(key); if (ctx != null && attribute == null) {
boolean alreadyIn = isTrue((Boolean) ctx.get(REQUEST_WRAPPER_GET_ATTRIBUTE)); // note: we don't let # come through or else a request for
// #attr.foo or #request.foo could cause an endless loop
if (!alreadyIn && !key.contains("#")) {
try {
// If not found, then try the ValueStack
ctx.put(REQUEST_WRAPPER_GET_ATTRIBUTE, Boolean.TRUE);
ValueStack stack = ctx.getValueStack();
if (stack != null) {
attribute = stack.findValue(key);
}
} finally {
ctx.put(REQUEST_WRAPPER_GET_ATTRIBUTE, Boolean.FALSE);
}
}
}
return attribute;
}
}

  从代码中可以看出,StrutsRequestWrapper重写了getAttribute,其中有一段核心代码如下:

                    ValueStack stack = ctx.getValueStack();
if (stack != null) {
attribute = stack.findValue(key);
}

  debug代码,可以看出${pName}等是从ValueStack中获取出来的

3、ValueStack

  首先这是一个接口,里面有两个关键声明方法

public abstract Map<String, Object> getContext();

public abstract CompoundRoot getRoot();

  debug代码,可以发现ValueStack包括两个核心属性

一个context,一个root,context里面包含的是一系列map,requestMap,applicationMap,sessionMap等;

root是一个CompoundRoot对象,其实也是个map,这里面包括的就是我们自己Action对象了

${pName}等正是从这里查出来的,并非从request中获取的。

 

struts2笔记10-值栈的更多相关文章

  1. Struts2笔记_值栈

    A.值栈概述 值栈(ValueStack),通俗的来说就是Struts2里面用来管理和存储数据的东西.struts2项目部署运行后,底层会创建一个action实例,同时也会在内存上划分一块区域,这个区 ...

  2. struts2中各种值栈问题

    struts2中OGNL和 ValueStack(一) 收藏 学习的时候,总分不清楚在struts2中页面的传值和取值是怎么来完成的,所以从网上搜了很多资料,现在把这些资料总结写,留着以后参考..看完 ...

  3. Struts2 中的值栈的理解

    通过对struts2的一段时间的接触,将自己对OGNL的核心值栈说说,值栈:简单的说,就是存放action的堆栈,当我们提交一个请求 道服务器端 action时,就有个堆栈,如果action在服务器端 ...

  4. 关于Struts2中的值栈与OGNL表达式

    1.1.1    OGNL概述: Object Graphic Navigation Language(对象图导航语言)的缩写 * EL     :OGNL比EL功能强大很多倍. 它是一个开源项目. ...

  5. struts2 ValueStack(值栈)解析

    Struts2一个重要点就是值栈. ValueStack,是用来存储一些在各个action,或者说是通过s标签.el表达式等给前台Jsp等页面展示的东西. ValueStack是一个接口,其内部接口非 ...

  6. Struts2中的值栈

    一 什么是值栈 值栈: struts2中提供的一种类似于域对象的工具, 用于struts2中的存值和取值. 每次访问Action的时候, 都会创建一个action对象, 而每个action对象中都存在 ...

  7. Struts2 框架的值栈

    1. OGNL 表达式 1.1 概述 OGNL(Object Graphic Navigation Language),即对象图导航语言; 所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个 ...

  8. struts2中的值栈对象ValueStack

    ValueStack, 即值栈对象. 值栈对象: 是整个struts数据存储的核心,或者叫中转站. 用户每次访问struts的action,都会创建一个Action对象.值栈对象.ActionCont ...

  9. struts2学习(7)值栈简介与OGNL引入

    一.值栈简介: 二.OGNL引入: com.cy.action.HelloAction.java: package com.cy.action; import java.util.Map; impor ...

  10. Struts2学习:值栈(value stack)

    1.index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %& ...

随机推荐

  1. VS2003.NET在文件中查找卡死

    不知怎么的,安装vs2003后,一点查找就卡死. 修复方法:修改devenv.exe的兼容性配置,勾选“禁用视觉主题”! 说实话,还真不知道这两者有什么关系?

  2. 利用ajax做的柱状图,线性统计图,饼状图

    柱状图,两个不同类型的数据 以下是html页面代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  3. oracle中job定时调用存储过程的实例

    使用job模拟定时从元数据表中抽取指定数据进入目标表的过程. 一.创建元数据表 --create table test_origianl create table test_original_data ...

  4. phpStudy速度慢的解决办法

    1.修改mysql数据库链接地址为ip地址127.0.0.1. 2.使用最新版本,这个坑了我好久时间.

  5. Android 展示键盘时候布局被修改的问题

    解决方法,在mainfest.xml中,对那个Activity加: <activity android:name=".activity.HomeActivity"androi ...

  6. Seeding--zoj2100

    Seeding Time Limit: 2 Seconds      Memory Limit: 65536 KB It is spring time and farmers have to plan ...

  7. codeforces 518C. Anya and Smartphone

    C. Anya and Smartphone time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. 解析XML【C#】

     1.XML元素XML元素包含一个开标记.元素中的数据.闭标记例如:<book>book name</book>其中book是元素名称  book name是元素数据元素名称区 ...

  9. python学习day3

    目录: 1.集合set 2.计数器 3.有序字典 4.默认字典 5.可命名元组 6.队列 7.深浅拷贝 8.函数 9.lambda表达式 10.内置函数 一.集合set set是一个无序且不重复的元素 ...

  10. Zigbee2007协议中文介绍

    Zigbee2007中文介绍ZigBee2007规范定义了ZigBee和ZigBee Pro两个特性集,全新的ZigBee 2007规范建立在ZigBee2006之上,不但提供了增强型的功能而且在某些 ...