struts2笔记10-值栈
2016-04-05 20:55
567 查看
1、问题
提交页面:
响应页面:
${pName},${pDesc},${pPrice},写法简单优雅,优雅的背后肯定有故事,struts2在背后帮我们做了些什么?
2、StrutsRequestWrapper
(1)打印出request看看
(2)StrutsRequestWrapper源码
从代码中可以看出,StrutsRequestWrapper重写了getAttribute,其中有一段核心代码如下:
debug代码,可以看出${pName}等是从ValueStack中获取出来的
3、ValueStack
首先这是一个接口,里面有两个关键声明方法
debug代码,可以发现ValueStack包括两个核心属性
一个context,一个root,context里面包含的是一系列map,requestMap,applicationMap,sessionMap等;
root是一个CompoundRoot对象,其实也是个map,这里面包括的就是我们自己Action对象了
${pName}等正是从这里查出来的,并非从request中获取的。
提交页面:
<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中获取的。
相关文章推荐
- 【转】深入剖析Java中的装箱和拆箱
- 用eclipse打包jar的时候有外部jar包
- java实现图的深度优先搜索算法
- Java数据泛型正确的初始化方法
- java中已经排序的列表中插入新值
- java中双向链表的增、删、查操作
- Java数据泛型正确的初始化方法
- MyEclipse快捷键大全
- hdu2045java递推
- java反射--总结 及 JavaBean--
- struts2图片文件上传经验总结
- Spring中@ImportResource和@Value加载资源文件
- Java第五次实验要求
- hdu2044java递推
- JAVA常用运算符极其运算优先级的总结
- Obj2002java
- java二分搜索算法
- Spring自动装配Bean属性的四种方式
- Objhdu2001java
- spring aop面向切面编程