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. 对WEB标准以及W3C的理解与认识 - 提高网页加载速度

    在写代码的时候应该注意: 1.标签闭合 2.标签小写 3.不能随意嵌套 提高被搜索引擎搜到几率: mate中的name变量[其中keywords和description尤其重要] Meta name= ...

  2. BeanUtils 以及BeanUtils.populate使用

    Apache Jakarta Commons项目非常有用.我曾在许多不同的项目上或直接或间接地使用各种流行的commons组件.其中的一个强大的组件就是BeanUtils.我将说明如何使用BeanUt ...

  3. jq插件又来了,模拟select下拉框,支持上下方向键哦

    好久没来了,更新下插件, 这个原理就是利用的 input[type='hidden']和自定义属性data-value捆绑传值操作的,可是设置默认选项,回调等参数,代码不多,比较简单,吼吼 (func ...

  4. 浅析 JavaScript 的函数节流和去抖

    现代网页的实现上,会有很多交互上的优化,比如常见的 滚动加载 ,输入联想 等等.他们的实现思路很简单,以滚动加载而言,无非就是去是增加一个滚动的事件监听,每次滚动判断当前的元素是否已经滚动到了用户的可 ...

  5. javascript的层次

    1.功能api 2.代码organization 3.performance 4.work flow

  6. C# 弗洛伊德(Floyd)算法

    弗洛伊德(Floyd)算法 主要是用于计算图中所有顶点对之间的最短距离长度的算法,如果是要求某一个特定点到图中所有顶点之间的最短距离可以用;        ;    ;    ;            ...

  7. JIRA官方:缺陷与事务跟踪

    快速跟踪软件缺陷 JIRA的网站页面使测试人员能够快速报告.管理.跟踪缺陷.使用键盘快捷键可以很容易地导航和修改问题.你还可以从IDE直接访问JIRA问题或者通过其它各种桌面客户端.手机客户端.浏览器 ...

  8. HDU2114 Calculate S(n) (取模算术)

    Calculate S(n) Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. linux内核交互,设备驱动控制管理接口

    1,ioctl preface--starting point ,format,mount volume,in addition to the above file system -- allows ...

  10. 一个月AS2.0总结。

    来这家公司一个月了,从最初学习它的木块,到流程,到组件,到改动,到自己做. 感觉好快. 1.AS2.0确实比較3.0差距太大.假设不是公司必须使用2.0,我是真不想使用. 2.代码重用性差.相同的代码 ...