一、前言

  很多事儿啊,就是“成也萧何败也萧何”,细想一些事儿心中有感,当然,感慨和本文毛关系都没有~想起之前有篇Struts2中值栈的博客还未完工,就着心中的波澜,狂咽一把~

二、正文

  博文基于:struts-core-2.5.2.jar,ognl-3.1.10.jar

  和值栈的邂逅还是大学学习Struts2的时候,老师当时在台上津津有味,我却在底下晕头转向~很长时间都没有去理清这个值栈到底是什么,实现原理是什么,偶然的际遇,让我又遇到了它,所以我决定以我现有的知识储备再次去追根溯源~

  那从概念上,我们要怎么去理解值栈呢?你可以将它理解为一个容器(就像装水的杯子,只不过它里面“装”的不是水,而是java对象),这边“装”的意思是“引用”了其他类或者对象,我想这样说应该能理解吧~其实呢,作为开发人员,再精准的文字描述也很难让自己理解到技术的精华,所以看源码是进阶的必经之路~源码的精妙有些时候会让人如痴如醉,编程,就应该是一门艺术~

  在Struts2中,值栈和ActionContext是同步创建的,这个创建过程可以在Struts2的核心过滤器:org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter中,doFilter方法中查看,源码如下:

 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

         HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res; try {
String uri = RequestUtils.getUri(request);
if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
LOG.trace("Request {} is excluded from handling by Struts, passing request to other filters", uri);
chain.doFilter(request, response);
} else {
LOG.trace("Checking if {} is a static resource", uri);
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled) {
LOG.trace("Assuming uri {} as a normal action", uri);
prepare.setEncodingAndLocale(request, response);
prepare.createActionContext(request, response);
prepare.assignDispatcherToThread();
request = prepare.wrapRequest(request);
ActionMapping mapping = prepare.findActionMapping(request, response, true);
if (mapping == null) {
LOG.trace("Cannot find mapping for {}, passing to other filters", uri);
chain.doFilter(request, response);
} else {
LOG.trace("Found mapping {} for {}", mapping, uri);
execute.executeAction(request, response, mapping);
}
}
}
} finally {
prepare.cleanupRequest(request);
}
}

  在上面的代码中,我们可以看到17行“prepare.createActionContext(request, response);”就是创建ActionContext的代码,其具体实现的源码如下:

 public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
ActionContext ctx;
Integer counter = 1;
Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
if (oldCounter != null) {
counter = oldCounter + 1;
} ActionContext oldContext = ActionContext.getContext();
if (oldContext != null) {
// detected existing context, so we are probably in a forward
ctx = new ActionContext(new HashMap<>(oldContext.getContextMap()));
} else {
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().putAll(dispatcher.createContextMap(request, response, null));
ctx = new ActionContext(stack.getContext());
}
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
ActionContext.setContext(ctx);
return ctx;
}

  进入14行中的createValueStack方法内部可以知道值栈的实现者为:OgnlValueStack

    //class OgnlValueStackFactory
public ValueStack createValueStack() {
ValueStack stack = new OgnlValueStack(xworkConverter, compoundRootAccessor, textProvider, allowStaticMethodAccess);
container.inject(stack);
stack.getContext().put(ActionContext.CONTAINER, container);
return stack;
}

  值栈OgnlValueStack中包含两个重要的成员变量,CompoundRoot root(这其实是一个ArrayList)和context.(我们通常称为contextMap):

 CompoundRoot root;
transient Map<String, Object> context; //其中CompoundRoot的声明如下
public class CompoundRoot extends CopyOnWriteArrayList<Object>

  再进入OgnlValueStack的构造函数和setRoot方法:

    protected OgnlValueStack(ValueStack vs, XWorkConverter xworkConverter, CompoundRootAccessor accessor, boolean allowStaticAccess) {
setRoot(xworkConverter, accessor, new CompoundRoot(vs.getRoot()), allowStaticAccess);
} protected void setRoot(XWorkConverter xworkConverter, CompoundRootAccessor accessor, CompoundRoot compoundRoot,
boolean allowStaticMethodAccess) {
this.root = compoundRoot;
this.securityMemberAccess = new SecurityMemberAccess(allowStaticMethodAccess);
this.context = Ognl.createDefaultContext(this.root, accessor, new OgnlTypeConverterWrapper(xworkConverter), securityMemberAccess);
context.put(VALUE_STACK, this);
Ognl.setClassResolver(context, accessor);
((OgnlContext) context).setTraceEvaluations(false);
((OgnlContext) context).setKeepLastEvaluation(false);
}

  从第11行我们可以看出,OgnlValueStack中的context有包含了对自身的引用,我们回头再来看看第10行Ognl.createDefaultContext方法内部的实现:

 //class Ognl.java
public static Map createDefaultContext(Object root, ClassResolver classResolver,TypeConverter converter, MemberAccess memberAccess)
{
return addDefaultContext(root, classResolver, converter, memberAccess, new OgnlContext());
} public static Map addDefaultContext(Object root, ClassResolver classResolver,TypeConverter converter, MemberAccess memberAccess, Map context)
{
OgnlContext result; if (!(context instanceof OgnlContext)) {
result = new OgnlContext();
result.setValues(context);
} else {
result = (OgnlContext) context;
}
if (classResolver != null) {
result.setClassResolver(classResolver);
}
if (converter != null) {
result.setTypeConverter(converter);
}
if (memberAccess != null) {
result.setMemberAccess(memberAccess);
} result.setRoot(root);
return result;
}

  从方法addDefaultContext可以得知,OnglValueStack中的context是OnglContext对象。第27行可以看出,这个context还包含了对OgnlValueStack中“CompoundRoot root”的引用。

  接下来,我们回到PrepareOperation.java类中的“createActionContext”方法(StrutsPrepareAndExecuteFilter.java中doFilter中调用这个方法,正文最开始已经贴出了源码):

 public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
ActionContext ctx;
Integer counter = 1;
Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
if (oldCounter != null) {
counter = oldCounter + 1;
} ActionContext oldContext = ActionContext.getContext();
if (oldContext != null) {
// detected existing context, so we are probably in a forward
ctx = new ActionContext(new HashMap<>(oldContext.getContextMap()));
} else {
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().putAll(dispatcher.createContextMap(request, response, null));
ctx = new ActionContext(stack.getContext());
}
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
ActionContext.setContext(ctx);
return ctx;
}

  从之前的源码分析,我们知道,14行的ValueStack存储的是OgnlValueStack对象,这个对象的getContext方法获取成员变量context(Map类型,具体实现类为OgnlContext)。我们再来看看15行dispatcher.createContextMap的源码:

 // class Dispacher.java

 public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
ActionMapping mapping) { // request map wrapping the http request objects
Map requestMap = new RequestMap(request); // parameters map wrapping the http parameters. ActionMapping parameters are now handled and applied separately
Map params = new HashMap(request.getParameterMap()); // session map wrapping the http session
Map session = new SessionMap(request); // application map wrapping the ServletContext
Map application = new ApplicationMap(servletContext); Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response); if (mapping != null) {
extraContext.put(ServletActionContext.ACTION_MAPPING, mapping);
}
return extraContext;
}

  看着上面Map类型的requestMap、params等变量,是不是觉得很熟悉?我们在使用ActionContext的时候是不是经常有如下用法,这些值就在这边设置的:

         ActionContext.getContext().getParameters();
ActionContext.getContext().get("request");
ActionContext.getContext().getSession();

  等等,是不是又有读者要问了,我值栈ValueStack中的context怎么又和ActionContext扯上关系了?有迷惑的读者,请往前两段代码看下createActionContext的第16行:

  再看下类ActionContext的声明和构造函数:

    .........................................................................................
public class ActionContext implements Serializable { static ThreadLocal<ActionContext> actionContext = new ThreadLocal<>();
private Map<String, Object> context; public ActionContext(Map<String, Object> context) {
this.context = context;
}
public static void setContext(ActionContext context) {
actionContext.set(context);
}
public static ActionContext getContext() {
return actionContext.get();
}
........................................................................
}

  OgnlValueStack将成员变量context传递给ActionContext对象的context成员变量,之前在OgnlValueStack设置的值便都能在ActionContext的对象中获取~当然这边还有一个非常关键的一步:

  这一步将ActionContext实例变量设置到ThreadLocal变量中,这个就使得ActionContext实例变量可以在同一个线程中的不同类间传递,对于ThreadLocal使用不太清晰的读者,请参考我之前的博文:ThreadLocal 验明正身

  至此,已经从源码的角度将Struts2中的“值栈”说清楚了~下面附张图,可以更好的帮助读者去理解本文内容~(别人画的,本文参考链接中有贴出原链接 )

(本图用来说明OnglValueStack中context和ActionContext中context的关系)

总结:

  当初会去深究Struts2中的值栈,起因是在使用Ognl表达式时,获取“值栈”中某些值,有些表达式要加“#”有些不要,然后就有了去深究的冲动~

  “值栈”由两部分组成:

  1)ObjectStack (保存为root属性,类型CompoundRoot) ----- ArrayList

  2)ContextMap(保存为context属性, 类型  ) ------ Map

Struts2 会把下面这些映射压入 ContextMap 中:

  parameters: 该 Map 中包含当前请求的请求参数
  request: 该 Map 中包含当前 request 对象中的所有属性
  session: 该 Map 中包含当前 session 对象中的所有属性
  application:该 Map 中包含当前 application 对象中的所有属性
  attr: 该 Map 按如下顺序来检索某个属性: request, session, application

  注:CompoucdRoot继承了ArrayList,实际上就是一个集合,用于存储元素的数据,OgnlContext实现了Map, 其中持有CompoucdRoot对象的引用,其key为_root

  在JSP页面内,通过 <s:property>等标签去访问值栈的数据,访问root中数据,不需要“#”,访问 Context中数据必须以“#”开始。
  当Struts2接受一个请求时,会迅速创建ActionContext,ValueStack,action ,然后把action存放进ValueStack,所以action的实例变量可以被OGNL访问。
Action对象会被保存root 对象栈中,Action所有成员变量属性,都位于root中 ,访问root中数据不需要“#”。

  再附一张图,图中“ValueStack(值栈,根对象)”指的是OgnlValueStack类中的CompoundRoot root成员变量:

三、链接

http://blog.csdn.net/javaliuzhiyue/article/details/9357337

http://www.cnblogs.com/x_wukong/p/3887737.html

http://blog.csdn.net/elvis12345678/article/details/7909936

http://www.cnblogs.com/hlhdidi/p/6185836.html

四、联系本人

  为方便没有博客园账号的读者交流,特意建立一个企鹅群(纯公益,非利益相关),读者如果有对博文不明之处,欢迎加群交流:261746360,小杜比亚-博客园

Struts2值栈的更多相关文章

  1. struts2值栈内部数据结构详解

    值栈是struts2内部一片很重要的区域,我在初学的时候,发现对于值栈这个数据结构的理解不是很深刻.例如OGNLContext是什么,ActionContext和值栈有什么关系.为什么ActionCo ...

  2. struts2值栈分析

    前段日子对ognl表达式不是很理解,看了几本书上关于ognl表达式的描述后还是感觉很难,前几天学习了struts2中值栈的内容,现在感觉ognl表达式其实很容易. struts2中利用值栈来存储数据, ...

  3. Struts2值栈详解

    1. 关于值栈: 1). helloWorld 时, ${productName} 读取 productName 值, 实际上该属性并不在 request 等域对象中, 而是从值栈中获取的.  2). ...

  4. 【基于初学者的SSH】struts2 值栈的详解与struts2标签库+ognl表达式

    一:什么是值栈:struts2里面本身提供的一种存储机制,类似于域对象,值栈,可以存值和取值 特点:先进后出,最上面的元素叫做栈顶,也叫压栈. <s:debug></s:debug& ...

  5. Struts2 - 值栈(ValueStack)

    1. 关于值栈: 1). helloWorld 时, ${productName} 读取 productName 值, 实际上该属性并不在 request 等域对象中, 而是从值栈中获取的. 2). ...

  6. struts2 值栈分析

    目录 一.值栈分为两个逻辑部分 二.Struts2 利用 s:property 标签和 OGNL表达式来读取值栈中的属性值 1.值栈中的属性值: 2.读取对象栈中对象的属性: 3.默认情况下,Acti ...

  7. 10.Struts2值栈

    1.什么是值栈 * 值栈就相当于Struts2框架的数据的中转站,向值栈存入一些数据.从值栈中获取到数据. * ValueStack 是 struts2 提供一个接口,实现类 OgnlValueSta ...

  8. struts2值栈ValueStack中都有哪些东西?

    com.opensymphony.xwork2.dispatcher.HttpServletRequest application com.opensymphony.xwork2.dispatcher ...

  9. Struts2 值栈总结(ValueStack)

    1.获取值栈 //获取值栈的第一种方式 ValueStack valueStack1 = (ValueStack) ServletActionContext.getRequest().getAttri ...

随机推荐

  1. Java 的标识接口作用

    原文地址:标识接口 作用作者:feisong 时间:2019-01-2315:49:35 标识接口是没有任何方法和属性的接口.标识接口不对实现它的类有任何语义上的要求,它仅仅表明实现它的类属于一个特定 ...

  2. vue $set修改对象

    在vue开发中,当生成vue实例后,再次给数据赋值时,有时候并不会自动更新到视图上去: eg:<!DOCTYPE html> <html> <head> <m ...

  3. Sencha Visual Studio(IDE插件)

    Sencha Visual Studio(IDE插件) 首先从官网上下载Visual Studio插件,注意不是VSCode编辑器,下载完后安装打开Visual Studio提示你去注册,输入你的se ...

  4. 基于GTID的MySQL主从复制#从原理到配置

    GTID是一个基于原始mysql服务器生成的一个已经被成功执行的全局事务ID,它由服务器ID以及事务ID组合而成.这个全局事务ID不仅仅在原始服务器器上唯一,在所有存在主从关系 的mysql服务器上也 ...

  5. ctf题目writeup(8)

    2019.2.11 南京邮电的ctf平台: 地址http://ctf.nuptzj.cn/challenges# 他们好像搭新的平台了...我注册弄了好半天... 1. 签到题,打开网址: 查看一下页 ...

  6. HyperLedger Fabric 1.4 架构(6.2)

    6.2.1 架构演进       Fabric架构经历了0.6版本到1.0版本的演进,架构上进行了重大改进,从0.6版本的结构简单演进到可扩展.多通道的设计,在架构上有了质的飞跃:从1.0版本以后,架 ...

  7. MySQL共享表空间扩容

    一.什么是共享表空间和独占表空间 共享表空间以及独占表空间都是针对数据的存储方式而言的. 共享表空间: 某一个数据库的所有的表数据,索引文件全部放在一个文件中,默认这个共享表空间的文件路径在data目 ...

  8. 初步学习pg_control文件之七

    接前文 初步学习pg_control文件之六  看   pg_control_version 以PostgreSQL9.1.1为了,其HISTORY文件中有如下的内容: Release Release ...

  9. 深度分析如何在Hadoop中控制Map的数量(摘抄)

    很多文档中描述,Mapper的数量在默认情况下不可直接控制干预,因为Mapper的数量由输入的大小和个数决定.在默认情况下,最终input占据了多少block,就应该启动多少个Mapper.如果输入的 ...

  10. svn 服务器搭建 (Linux)

    1.svn目前在程序开发工程汇总应用非常普遍,所以学习svn的环境搭建还是很有必要的 2.本次安装的服务是Subversion(svn)针对的环境是Linux,Subversion(SVN) 是一个开 ...