您的位置:首页 > 编程语言 > Java开发

Struts2 ValueStack & ActionContext & OGNL 关系小结

2012-05-13 00:31 726 查看
  个人认为,Struts2里面的ValueStack概念解释的比较混乱,有时候是ValueStack,有时候又特指其中的ObjectStack。导致在使用OGNL表达式的时候出现一些问题。还有一个问题是,Struts2扩展了一些原始OGNL的概念,导致一些令人迷惑的问题。现对照一些原生OGNL的例子及Struts2的源码分析问题出现原因,消除心中的疑惑。

1.原生的OGNL

 1) Introduction

  OGNL stands for Object-Graph Navigation Language; it is an expression language for getting and setting properties of Java objects, plus other extras such as list projection and selection and lambda expressions. You use the same expression for both getting and setting the value of a property.

  We pronounce OGNL as a word, like the last syllables of a drunken pronunciation of "orthogonal".

  Many people have asked exactly what OGNL is good for. Several of the uses to which OGNL has been applied are:

A binding language between GUI elements (textfield, combobox, etc.) to model objects. Transformations are made easier by OGNL's TypeConverter mechanism to convert values from one type to another (String to numeric types, for example);

A data source language to map between table columns and a Swing TableModel;

A binding language between web components and the underlying model objects;

A more expressive replacement for the property-getting language used by the Apache Commons BeanUtils package or JSTL's EL (which only allow simple property navigation and rudimentary indexed properties).

  OGNL常被用来做为View与Model之间的胶水语言,在View中方便的使用Model对象,OGNL负责收集这些Model对象,在View中使用简单的OGNL表达式,就可以取到想要的对象。既消除了在View中直接使用getters 和 setters的重复代码的书写,还避免了在View代码中写的强制类型转换(Cast)。

  表达式语言(EL)本质上被设计为:帮助你使用简单的表达式来完成一些“常用”的工作。通常情况下,ELs 可以在一些框架中找到,它被是用来简化我们的工作。例如:大家熟知的 Hibernate,使用 HQL(Hibernate Query Language) 来完成数据库的操作,HQL 成了开发人员与复查的 SQL 表达式之间的一个桥梁。 在 web 框架下,表达式语言起到了相似的目的。它的存在消除了重复代码的书写。

 2) Use OGNL

static Object
getValue(Object tree, Map context, Object root)

Evaluates the given OGNL expression tree to extract a value from the given root object.
static Object
getValue(Object tree, Map context, Object root, Class resultType)

Evaluates the given OGNL expression tree to extract a value from the given root object.
static Object
getValue(Object tree, Object root)

Evaluates the given OGNL expression tree to extract a value from the given root object.
static Object
getValue(Object tree, Object root, Class resultType)

Evaluates the given OGNL expression tree to extract a value from the given root object.
static Object
getValue(String expression, Map context, Object root)

Evaluates the given OGNL expression to extract a value from the given root object in a given context
static Object
getValue(String expression, Map context, Object root, Class resultType)

Evaluates the given OGNL expression to extract a value from the given root object in a given context
static Object
getValue(String expression, Object root)

Convenience method that combines calls to
parseExpression
and
getValue
.
static Object
getValue(String expression, Object root, Class resultType)

Convenience method that combines calls to
parseExpression
and
getValue
.
  使用OGNL非常简单,我们只用到了Ognl.jar文件,并且只使用了其中的一个类ognl.Ognl。这个类中的方法都是静态static方法。取值只用到了getValue方法,这个方法是个重载overload方法。

  OGNL三要素:

  1.expression 求值表达式——首先会被解析成对象树

  2.root object 根对象——默认的操作对象

  3.context OGNL执行环境——OGNL执行的上下文环境

  OGNL context是一个Map结构,ognl.OgnlContext类implements Map接口,root对象也在context里面,并且做这一个特殊的对象处理,具体表现为对root 对象的操作不需要加#指示符号(并且加上了#一定取不到root对象里面的值)。

  看一个例子:

Person类:

package com.cuillgln.ognl;

public class Person {
private String name;
private int age;
private String telphone;

public Person() {

}

public Person(String name, int age, String telphone) {
this.name = name;
this.age = age;
this.telphone = telphone;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getTelphone() {
return telphone;
}

public void setTelphone(String telphone) {
this.telphone = telphone;
}

}


OgnlTest类:

package com.cuillgln.ognl;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class OgnlTest {

public static void main(String[] args) {
try {
OgnlContext context = new OgnlContext();
context.put("name", "STRING1~~~");
context.put("age", 100);
context.put("telphone", "STRING3~~~");
context.put("person.name", "STRING4~~~");

Person p1 = new Person("Lee", 12, "134xxxxxxxx");
Person p2 = new Person("Zhang", 15, "150xxxxxxxx");
Person p3 = new Person("Wang", 18, "186xxxxxxxx");

context.put("person1", p1);
context.put("person2", p2);
context.put("person3", p3);

Person root = new Person("ROOT", 100, "xxxxxxxx");

/*
* the output is :
* the value is: ROOT======
* obviously, value是对应root object 的name属性的对象。
* 输出的是这个对象的值
*/
Object value = Ognl.getValue("name", context, root);
System.out.println("the value is: " + value + "======");

/*
* the output is :
* the value is: STRING1~~~======
* obviously, value是context中,Key为name的对象
* 输出的是这个对象的值
*/
value = Ognl.getValue("#name", context, root);
System.out.println("the value is: " + value + "======");

/*
* the output is :
* ognl.NoSuchPropertyException: com.cuillgln.ognl.Person.person1
* obviously, OGNL会试图去找root object中属性为person1的对象,
* 结果没有找到,所以抛出ognl.NoSuchePropertyException
*/
value = Ognl.getValue("person1", context, root);
System.out.println("the value is: " + value + "======");

/*
* the output is :
* the value is: com.cuillgln.ognl.Person@7d8483======
* obviously, value是context中,Key为person1的对象。
* 输出的结果是这个对象的toString的返回值
*/
value = Ognl.getValue("#person1", context, root);
System.out.println("the value is: " + value + "======");

/*
* the output is :
* ognl.NoSuchPropertyException: com.cuillgln.ognl.Person.person1
* obviously, OGNL会试图去找root object中属性为person1的对象,然后再找属性person1对应对象的name属性对应的对象
* 结果没有找到root object中有person1属性对应的对象,
* 所以抛出ognl.NoSuchePropertyException: com.cuillgln.ognl.Person.person1
*/
value = Ognl.getValue("person1.name", context, root);
System.out.println("the value is: " + value + "======");

/*
* the output is :
* the value is: Lee======
* obviously, OGNL会试图去找context中Key为person1对应的对象,再对这个对象取name属性对应的对象
* 结果找到了正确输出了……。Lee
*/
value = Ognl.getValue("#person1.name", context, root);
System.out.println("the value is: " + value + "======");

/*
* the output is :
* ognl.OgnlException: source is null for getProperty(null, "name")
* obviously, OGNL会试图去找context中Key为person对应的对象,再对这个对象取name属性对应的对象
* 结果在context中没有找到Key为person的属性,故返回null,在null上再取name属性,明显会出错。
*/
value = Ognl.getValue("#person.name", context, root);
System.out.println("the value is: " + value + "======");

/*
* the output is :
* ognl.NoSuchPropertyException: java.lang.String.name
* obviously, OGNL会试图在root object中查找name属性对应的对象,
* 现在root object是一个String字符串,且没有name属性,故会抛出ognl.NoSuchPropertyException
*/
value = Ognl.getValue("name", context, "abcdefg");
System.out.println("the value is: " + value + "======");

/*
* the output is :
* the value is: STRING1~~~======
* obviously, value是context中,Key为name的对象,跟root object没有关系
* 输出的是这个对象的值
*/
value = Ognl.getValue("#name", context, "abcdefg");
System.out.println("the value is: " + value + "======");

} catch (OgnlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


  这些就是OGNL的基础知识,OGNL另外还有很强大的功能,包括投影、支持lambda表达式,暂不做介绍。

 2.XWork-specific OGNL features

  1)There can be many "root" objects. XWork中的表示根对象是CompoundRoot对象。CompoundRoot类extends ArrayList类。因为是一个List,里面可以放置多个对象,而这些对象经过XWork的改进对于OGNL表达式引擎来说都是root objects。XWork has a special OGNL PropertyAccessor that will automatically look at the all entries in the stack (in fact the CompoundRoot list) (from the top down) until it finds an object with the property you are looking for.

  2)Struts2 Named Objects

  Struts 2 places request parameters and request, session, and application attributes on the OGNL stack (in fact the OGNL context). They may be accessed as shown below.

namevalue
#parameters['foo'] or #parameters.foorequest parameter ['foo'] (request.getParameter())
#request['foo'] or #request.foorequest attribute ['foo'] (request.getAttribute())
#session['foo'] or #session.foosession attribute 'foo'
#application['foo'] or #application.fooServletContext attributes 'foo'
#attr['foo'] or #attr.fooAccess to PageContext if available, otherwise searches request/session/application respectively
 以OGNL表达式的观点来对上述两点做出解释:

  1)的意思是:OGNL中的root object,现在是CompoundRoot对象,CompoundRoot对象并不直接在里面放置属性(指JavaBean中的Field),而是一些对象列表。如果要用原生的OGNL表达式获取里面某个对象的值,会出现什么状况?你会发现根本取不到里面的对象。如下面的代码抛出的异常: ognl.NoSuchPropertyException: java.util.ArrayList.person。所以XWork在使用OGNL前做了一些特殊处理,具体就是使用了一个特殊的PropertyAccessor来访问CompoundRoot里面的对象。(具体没有研究……)

Person p1 = new Person("Lee", 12, "134xxxxxxxx");
Person p2 = new Person("Zhang", 15, "150xxxxxxxx");
Person p3 = new Person("Wang", 18, "186xxxxxxxx");

context.put("person1", p1);
context.put("person2", p2);
context.put("person3", p3);

List<Object> root = new ArrayList<Object>();
root.add(p3);
root.add(p2);
root.add(p1);
/*
* the output is :
* ognl.NoSuchPropertyException: java.util.ArrayList.person
* 你会发现很难用表达式,表达出来你要查找的对象(导航到你要找的对象)————无解……
*/
Object value = Ognl.getValue("person.name", context, root);
System.out.println("the value is: " + value + "======");


  2)的意思是:Struts把跟Web相关的对象,具体就是request parameters, request attributes, session attributes, application attributes。放在OGNL的context中,分别对应parameters, request, session, application的Key。attributes都以Key/Value的形式存在的,所以在OGNL context里key等于parameters, request, session, application的Value对应的对象类型是一个Map类。

3.OgnlValueStack的结构

  OgnlValueStack类implements ValueStack接口。里面有两个Field: context和root,分别是OGNL的两大要素OGNL context 和root object。

public class OgnlValueStack implements Serializable, ValueStack, ClearableValueStack, MemberAccessValueStack {
CompoundRoot root;
transient Map<String, Object> context;
Class defaultType;
transient OgnlUtil ognlUtil;
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);
}
/**
* @see com.opensymphony.xwork2.util.ValueStack#getContext()
*/
public Map<String, Object> getContext() {
return context;
}
/**
* @see com.opensymphony.xwork2.util.ValueStack#getRoot()
*/
public CompoundRoot getRoot() {
return root;
}
/**
* @see com.opensymphony.xwork2.util.ValueStack#findString(java.lang.String)
*/
public String findString(String expr) {
return (String) findValue(expr, String.class);
}

public String findString(String expr, boolean throwExceptionOnFailure) {
return (String) findValue(expr, String.class, throwExceptionOnFailure);
}

/**
* @see com.opensymphony.xwork2.util.ValueStack#findValue(java.lang.String)
*/
public Object findValue(String expr, boolean throwExceptionOnFailure) {
try {
setupExceptionOnFailure(throwExceptionOnFailure);
return tryFindValueWhenExpressionIsNotNull(expr);
} catch (OgnlException e) {
return handleOgnlException(expr, throwExceptionOnFailure, e);
} catch (Exception e) {
return handleOtherException(expr, throwExceptionOnFailure, e);
} finally {
ReflectionContextState.clear(context);
}
}

private Object tryFindValueWhenExpressionIsNotNull(String expr) throws OgnlException {
if (expr == null) {
return null;
}
return tryFindValue(expr);
}

private Object tryFindValue(String expr) throws OgnlException {
Object value;
expr = lookupForOverrides(expr);
if (defaultType != null) {
value = findValue(expr, defaultType);
} else {
value = getValueUsingOgnl(expr);
if (value == null) {
value = findInContext(expr);
}
}
return value;
}

private Object getValueUsingOgnl(String expr) throws OgnlException {
try {
return ognlUtil.getValue(expr, context, root);
} finally {
context.remove(THROW_EXCEPTION_ON_FAILURE);
}
}

public Object findValue(String expr) {
return findValue(expr, false);
}

/**
* @see com.opensymphony.xwork2.util.ValueStack#findValue(java.lang.String, java.lang.Class)
*/
public Object findValue(String expr, Class asType, boolean throwExceptionOnFailure) {
try {
setupExceptionOnFailure(throwExceptionOnFailure);
return tryFindValueWhenExpressionIsNotNull(expr, asType);
} catch (OgnlException e) {
return handleOgnlException(expr, throwExceptionOnFailure, e);
} catch (Exception e) {
return handleOtherException(expr, throwExceptionOnFailure, e);
} finally {
ReflectionContextState.clear(context);
}
}

private Object tryFindValueWhenExpressionIsNotNull(String expr, Class asType) throws OgnlException {
if (expr == null) {
return null;
}
return tryFindValue(expr, asType);
}

private Object handleOgnlException(String expr, boolean throwExceptionOnFailure, OgnlException e) {
Object ret = findInContext(expr);
if (ret == null) {
if (shouldLogNoSuchPropertyWarning(e)) {
LOG.warn("Could not find property [" + ((NoSuchPropertyException) e).getName() + "]");
}
if (throwExceptionOnFailure) {
throw new XWorkException(e);
}
}
return ret;
}
private Object tryFindValue(String expr, Class asType) throws OgnlException {
Object value = null;
try {
expr = lookupForOverrides(expr);
value = getValue(expr, asType);
if (value == null) {
value = findInContext(expr);
}
} finally {
context.remove(THROW_EXCEPTION_ON_FAILURE);
}
return value;
}

private Object findInContext(String name) {
return getContext().get(name);
}

public Object findValue(String expr, Class asType) {
return findValue(expr, asType, false);
}

  private Object getValue(String expr, Class asType) throws OgnlException {
    return ognlUtil.getValue(expr, context, root, asType);
  }

/**
* @see com.opensymphony.xwork2.util.ValueStack#peek()
*/
public Object peek() {
return root.peek();
}

/**
* @see com.opensymphony.xwork2.util.ValueStack#pop()
*/
public Object pop() {
return root.pop();
}

/**
* @see com.opensymphony.xwork2.util.ValueStack#push(java.lang.Object)
*/
public void push(Object o) {
root.push(o);
}

  public void set(String key, Object o) {
    //set basically is backed by a Map pushed on the stack with a key being put on the map and the Object being the value
    Map setMap = retrieveSetMap();
    setMap.put(key, o);
  }


  private Map retrieveSetMap() {
    Map setMap;
    Object topObj = peek();
    if (shouldUseOldMap(topObj)) {
      setMap = (Map) topObj;
    } else {
      setMap = new HashMap();
      setMap.put(MAP_IDENTIFIER_KEY, "");
      push(setMap);//放在了CompoundRoot的顶部
    }
    return setMap;
  }

  /**
   * check if this is a Map put on the stack for setting if so just use the old map (reduces waste)
   */
  private boolean shouldUseOldMap(Object topObj) {
    return topObj instanceof Map && ((Map) topObj).get(MAP_IDENTIFIER_KEY) != null;
  }

}


  从源码中可以看出:

  1)对ValueStack的栈操作(即peek、pop、push)实际上是对root上的操作。————这就是为什么说有时候ValueStack特指Object Stack。这里的Object Stack即是CompoundRoot对象,它提供了对栈的操作接口。

  2)findValue的过程:经过一系列的方法,最后调用tryFindValue方法,tryFindValue首先调用getValue,getValue调用OgnlUtil.getValue,OgnlUtil.getValue调用原生OGNL的Ognl.getValue方法。如果得到的value是null,会再调用findInContext方法,findInContext返回getContext().get(name),这是试图从context里获得Key为name的Value值。这是的name可能很复杂,如"person.name",此时不会再被解释成“先取得属性名为person的对象,再取这个对象的name属性对应的对象”,而是直接做为Map.get()里的一个Key。

  3)ValueStack.set(String key, Object value);确实是在CompoundRoot栈顶压入了一个HashMap,里面放入了set的Key/value pairs。

4.ActionContext类结构及初始化过程

  ActionContext里只有一个Filed,即context。从FilterDispacher的doFilter方法中,可以看出,ActionContext中的context被初始化为ValueStack中context的内容。并且在后面又在context中添加了一些key/value pairs。主要是把从ValueStack的context中的request parameters, request attributes, session attributes, application attributes,又以不同的Key放在了context中。并且还把整个ValueStack对象也放到了context中,Key是 "com.opensymphony.xwork2.util.ValueStack.ValueStack"。并且提供了这些对象的setters/getters方法。

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
ServletContext servletContext = getServletContext();

String timerKey = "FilterDispatcher_doFilter: ";
try {
 //1 处理前的准备
//1.1 创建值栈对象,值栈包含 object stack 和 context map 两个部分。
      ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
//1.2 创建 actionContext。
ActionContext ctx = new ActionContext(stack.getContext());
ActionContext.setContext(ctx);

// 省略剩余代码


5.use Struts tags with OGNL expression

  自定义标签(Customer Tags)一般都继承自javax.servlet.jsp.tagext.BodyTagSupport类,重写(overriding)doStartTag()和doEndTag()方法。

  对于像<s:set var="test" value="%{'abc'}" scope="scope"/> assigns a value to a variable in a specified scope。根据源代码可以看出来实际上是把var定义的key放到了context中相应的scope下的Map中或context中。调用的是ValueStack.setValue(expr, Object)方法,而不是ValueStac.set(key, value)方法。两方法的区别:

  1)ValueStac.set(key, value)是在CompoundRoot栈顶压入了一个HashMap,里面放入了set的Key/value pairs。

  2)ValueStack.setValue(expr, object)是给根据expr查找到的对象赋与object值。

  这就可以理解为什么在用<s:set/>绑定一个变量后,一般还是要加#才能取到值的原因了。因为他们的key/value pairs放置在context中,而不是CompoundRoot中。

  还要理解为什么有一部分变量就不用加#也可以取到。不加#可以取到条件有(三个条件,缺一不可):

    1) <s:set scope="action"/>中scope的值是默认值,即action作用域。

    2)<s:set value=""/>中value是个OGNL constants. 即String, 各种Number, Boolean, null等。

    3)在CompoundRoot对象中没有名为此key的属性。

  为什么能取到:

    1)scope="action"是把key/value pairs直接ValueStack.getContext().put(key, value)的。是存放在OGNL context中的。

    2)按原生的OGNL,不加#号是不可能取到的。

    3)Struts中的OgnlValueStack的tryFindValue()在用原生的OGNL getValue()得到的是null后(说明在CompoundRoot中没有与此key同名的属性),会调用findInContext(key)方法return getContext.get(key)。如此,如果在context中有此key,便可不用#也可能取到想要的值。

public boolean end(Writer writer, String body) {
ValueStack stack = getStack();

Object o;
if (value == null) {
if (body != null && !body.equals("")) {
o = body;
} else {
o = findValue("top");
}
} else {
o = findValue(value);
}

body="";

if ("application".equalsIgnoreCase(scope)) {
stack.setValue("#application['" + getVar() + "']", o);
} else if ("session".equalsIgnoreCase(scope)) {
stack.setValue("#session['" + getVar() + "']", o);
} else if ("request".equalsIgnoreCase(scope)) {
stack.setValue("#request['" + getVar() + "']", o);
} else if ("page".equalsIgnoreCase(scope)) {
stack.setValue("#attr['" + getVar() + "']", o, false);
} else {
stack.getContext().put(getVar(), o);
stack.setValue("#attr['" + getVar() + "']", o, false);
}

return super.end(writer, body);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: