struts2笔记10-值栈
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-值栈的更多相关文章
- Struts2笔记_值栈
A.值栈概述 值栈(ValueStack),通俗的来说就是Struts2里面用来管理和存储数据的东西.struts2项目部署运行后,底层会创建一个action实例,同时也会在内存上划分一块区域,这个区 ...
- struts2中各种值栈问题
struts2中OGNL和 ValueStack(一) 收藏 学习的时候,总分不清楚在struts2中页面的传值和取值是怎么来完成的,所以从网上搜了很多资料,现在把这些资料总结写,留着以后参考..看完 ...
- Struts2 中的值栈的理解
通过对struts2的一段时间的接触,将自己对OGNL的核心值栈说说,值栈:简单的说,就是存放action的堆栈,当我们提交一个请求 道服务器端 action时,就有个堆栈,如果action在服务器端 ...
- 关于Struts2中的值栈与OGNL表达式
1.1.1 OGNL概述: Object Graphic Navigation Language(对象图导航语言)的缩写 * EL :OGNL比EL功能强大很多倍. 它是一个开源项目. ...
- struts2 ValueStack(值栈)解析
Struts2一个重要点就是值栈. ValueStack,是用来存储一些在各个action,或者说是通过s标签.el表达式等给前台Jsp等页面展示的东西. ValueStack是一个接口,其内部接口非 ...
- Struts2中的值栈
一 什么是值栈 值栈: struts2中提供的一种类似于域对象的工具, 用于struts2中的存值和取值. 每次访问Action的时候, 都会创建一个action对象, 而每个action对象中都存在 ...
- Struts2 框架的值栈
1. OGNL 表达式 1.1 概述 OGNL(Object Graphic Navigation Language),即对象图导航语言; 所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个 ...
- struts2中的值栈对象ValueStack
ValueStack, 即值栈对象. 值栈对象: 是整个struts数据存储的核心,或者叫中转站. 用户每次访问struts的action,都会创建一个Action对象.值栈对象.ActionCont ...
- struts2学习(7)值栈简介与OGNL引入
一.值栈简介: 二.OGNL引入: com.cy.action.HelloAction.java: package com.cy.action; import java.util.Map; impor ...
- Struts2学习:值栈(value stack)
1.index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %& ...
随机推荐
- oracle 之 COMMENT
http://blog.csdn.net/liguihan88/article/details/3002403 无疑注释现在都被大家接受和认可,在大家编程用的IDE中都提供或有第三方插件来支持提取注释 ...
- <link>: rel, href
Reference: http://www.w3schools.com/tags/tag_link.asp <link> Attributes: Attribute Value Descr ...
- Lotto--poj2245
Lotto Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6605 Accepted: 4185 Description ...
- Hadoop伪分布式搭建步骤
说明: 搭建环境是VMware10下用的是Linux CENTOS 32位,Hadoop:hadoop-2.4.1 JAVA :jdk7 32位:本文是本人在网络上收集的HADOOP系列视频所附带的 ...
- StringBuilder[] 作为数组如何使用
在工作中突然要用到这个就记录下来. 不知为何我这里的StringBuilder[] 数组必须要指明几个(les)才给用,否则就会报错. int les = 5; StringBuilder[] sb_ ...
- Oracle EBS-SQL (CST-3):检查零成本交易.sql
SELECT '零成本交易' 交易异常类型 ,msi.segment1 ...
- Linux中Samba详细安装
为了实现Windows主机与Linux服务器之间的资源共享,Linux操作系统提供了Samba服务,Samba服务为两种不同的操作系统架起了一座桥梁,使Linux系统和Windows系统之间能够实现互 ...
- hdu4331 Image Recognition 就暴力啊。。啊。。
题意: 给一个1和0组成的正方形矩阵,求 四条边都由1构成的正方形的个数. 方法: 先统计矩阵中每一点,向四个方向,最多有多少个连续的1,这里用dp做也 与此同时,顺便求下 能向右下和 左上 两个方向 ...
- 创建渐进式jpeg图片
<?php // Create an image instance $im = imagecreatefromjpeg('test.jpg'); // Enable interlancing ...
- linux使用FIO测试磁盘的iops
FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...