`
andismail
  • 浏览: 2840 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Struts2中OGNL,valueStack,stackContext的学习(转)

 
阅读更多
[size=large]
<s:form action="hello/converter.action" method="post">   
        <s:textfield name="point" label="点"></s:textfield>    
        <s:textfield name="point2" label="2"></s:textfield>    
        <s:textfield name="point3" label="3"></s:textfield>    
        <s:textfield name="age" label="年龄"></s:textfield>    
        <s:textfield name="date" label="日期"></s:textfield>    
        <s:submit name="提交"></s:submit>    
    </s:form>  

 
<s:form action="hello/converter.action" method="post">   
        <s:textfield name="point" label="点"></s:textfield>   
        <s:textfield name="point2" label="2"></s:textfield>   
        <s:textfield name="point3" label="3"></s:textfield>   
        <s:textfield name="age" label="年龄"></s:textfield>   
        <s:textfield name="date" label="日期"></s:textfield>   
        <s:submit name="提交"></s:submit>   
    </s:form> 
<s:form action="hello/converter.action" method="post">
		<s:textfield name="point" label="点"></s:textfield>
		<s:textfield name="point2" label="2"></s:textfield>
		<s:textfield name="point3" label="3"></s:textfield>
		<s:textfield name="age" label="年龄"></s:textfield>
		<s:textfield name="date" label="日期"></s:textfield>
		<s:submit name="提交"></s:submit>
	</s:form>



结果图(是通过<s:debug></s:debug>得到的)
value stack:



Stack context:







通过图中我们可以看到
valuestack中包括我传递的值(point,point2,point3,age,date)(即action用字段保存的值,action中有point等字段的set,get方法) 可直接访问无须用#



stack context中包括了 request application OgnlValueStack(root) session parameters 等属性 (一般stack context文中都是一个Map),访问必须用#


这些valuestack中的值也可以通过stack context获取如:#parameters.point.

例:在jsp页面中累加计算

<ww:set name="pageIndex" value="1"></ww:set>

<ww:set name="pageIndex" value="#pageIndex+1"></ww:set>

上面pageIndex在action中没有字段set和ge方法.该pageIndex是保存在pageContext中.所以该pageContext中是保存在stack context中,所以访问该pageIndex必须用#.
值栈(ValueStack)    
Struts2将OGNL上下文设置为Struts2中的ActionContext(内部使用的仍然是OgnlContext),并将值栈设为OGNL的根对象。    
我们知道,OGNL上下文中的根对象可以直接访问,不需要使用任何特殊的“标记”,而引用上下文中的其他对象则需要使用“#”来标记。由于值栈是上下文中的根对象,因此可以直接访问。那么对于值栈中的对象该如何访问呢?Struts2提供了一个特殊的OGNLPropertyAccessor,它可以自动查找栈内的所有对象(从栈顶到栈底),直接找到一个具有你所查找的属性的对象。也就是说,对于值栈中的任何对象都可以直接访问,而不需要使用“#”。    
假设值栈中有两个对象:student和employee,两个对象都有name属性,student有学号属性number,而employee有薪水属性salary。employee先入栈,student后入栈,位于栈顶,那么对于表达式name,访问的就是student的name属性,因为student对象位于栈顶;表达式salary,访问的就是employee的salary属性。正如你所见,访问值栈中的对象属性或方法,无须指明对象,也不用“#”,就好像值栈中的对象都是OGNL上下文中的根对象一样。这就是Struts2在OGNL基础上做出的改进。

值栈中的Action实例  
Struts2框架总是把Action实例放在栈顶。因为Action在值栈中,而值栈又是OGNL中的根,所以引用Action的属性可以省略“#”标记,这也是为什么我们在结果页面中可以直接访问Action的属性的原因。

Struts2中的命名对象    
Struts2还提供了一些命名对象,这些对象没有保存在值栈中,而是保存在ActionContext中,因此访问这些对象需要使用“#”标记。这些命名对象都是Map类型。   

parameters    
用于访问请求参数。如:#parameters['id']或#parameters.id,相当于调用了HttpServletRequest对象的getParameter()方法。    
注意,parameters本质上是一个使用HttpServletRequest对象中的请求参数构造的Map对象,一量对象被创建(在调用Action实例之前就已经创建好了),它和HttpServletRequest对象就没有了任何关系。 
  
request    
用于访问请求属性。如:#request['user']或#request.user,相当于调用了HttpServletRequest对象的getAttribute()方法。   

session    
用于访问session属性。如:#session['user']或#session.user,相当于调用了HttpSession对象的getAttribute()方法。   

application    
用于访问application属性。如:#application['user']或#application.user,相当于调用了ServletContext的getAttribute()方法。   

attr    
如果PageContext可用,则访问PageContext,否则依次搜索request、session和application对象。

以下是转过来的:先分清楚下ActionContext 、ValueStack 、Stack Context三者

ActionContext
一次Action调用都会创建一个ActionContext
调用:ActionContext context = ActionContext.getContext()

ValueStack
由OGNL框架实现
可以把它简单的看作一个栈(List) 。

Stack Object:放入stack中的对象,一般是action。
Stack Context(map):stack上下文,它包含一系列对象,包括request/session/attr/application map等。
EL:存取对象的任意属性,调用对象的方法,遍历整个对象结…

ActionContext是Action上下文,可以得到request session application
ValueStack是值栈 存放表单中的值
Stack Context 栈上下文 也是用来存值的



struts2对OGNL上下文的概念又做了进一步扩充,在struts2中,OGNL上下文通常如下所示:

                        |--request  

                        |  

                        |--application  

                        |  

context map---|--OgnlValueStack(root) [ user, action, OgnlUtil, ... ]  

                        |  

                        |--session  

                        |  

                        |--attr  

                        |  

                        |--parameters 




    在Struts2中,采用标准命名的上下文(Context)来处理OGNL表达式。处理OGNL的顶级对象是一个Map(也叫context map),而OGNL在这个context中就是一个顶级对象(root)。在用法上,顶级对象的属性访问,是不需要任何标记前缀的。而其它非顶级的对象访问,需要使用#标记。
    Struts2框架把OGNL Context设置为我们的ActionContext。并且ValueStack作为OGNL的根对象。除value stack之外,Struts2框架还把代表application、session、request这些对象的Map对象也放到ActionContext中去。(这也就是Struts2建议在Action类中不要直接访问Servlet API的原因,他可以通过ActionContext对象来部分代替这些(Servlet API)功能,以方便对Action类进行测试!

即struts2.0使用如下方式:ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST))
    Action的实例,总是放到value stack中。因为Action放在stack中,而stack是root(根对象),所以对Action中的属性的访问就可以省略#标记。但是,要访问ActionContext中其它对象的属性,就必须要带上#标记,以便让OGNL知道,不是从根对象,而是从其它对象中去寻找。
    那么访问Action中的属性的代码就可以这样写

<s:property value="postalCode"/>
    其它ActionContext中的非根对象属性的访问要像下面这样写:
<s:property value="#session.mySessionPropKey"/> or
<s:property value="#session['mySessionPropKey']"/> or
<s:property value="#request['myRequestPropKey']"/>
    对Collection的处理,内容就很简单。
<s:select label="label" name="name" list="{'name1','name2','name3'}" value="%{'name2'}" />
    这是处理List。这个代码在页面上建立一个下拉选项,内容是list中的内容,默认值是name2.
处理map

<s:select label="label" name="name" list="#{'foo':'foovalue', 'bar':'barvalue'}" />

     需要注意的是,判断一个值是否在collection中。我们要使用in或者not in来处理。
<s:if test="'foo' in {'foo','bar'}">
   muhahaha
</s:if>
<s:else>
   boo
</s:else>
另外,可以使用通配符来选择collection对象的子集。
?——所有匹配选择逻辑的元素
^——只提取符合选择逻辑的第一个元素
$——只提取符合选择逻辑的最后一个元素
person.relatives.{? #this.gender == 'male'}

`````````````````````````````````````````````````````````````````````````````````

以下为补充摘录的一些问题:

提问:在Struts2中,如何使用自身的Tag读取Action中的变量?

Struts2自身的Tag会根据value中的OGNL表达式,在ValueStack中寻找相应的对象。因为action在ValueStack的顶部,所以默认情况下,Struts2的Tag中的OGNL表达式将查找action中的变量。请注意,value中的内容直接是OGNL表达式,无需任何el的标签包装。

例如:<s:property value="user.name" />

提问:在Struts2中,如何使用自身的Tag读取HttpServletRequest,HttpSession中的变量?

在上面的知识中,我们知道,Struts2中OGNL的上下文环境中,包含request,session,application等servlet对象的Map封装。既然这些对象都在OGNL的上下文中,那么根据OGNL的基本知识,我们可以通过在表达式前面加上#符号来对这些变量的值进行访问。

例如:<s:property value="%{#application.myApplicationAttribute}" />
<s:property value="%{#session.mySessionAttribute}" />
<s:property value="%{#request.myRequestAttribute}" />
<s:property value="%{#parameters.myParameter}" />



提问:在Struts2中,如何使用JSTL来读取Action中的变量?

这是一个历史悠久的问题。因为事实上,很多朋友(包括我在内)是不使用Struts2自身的标签库,而是使用JSTL的,可能因为JSTL标签库比较少,简单易用的原因吧。

我们知道,JSTL默认是从page,request,session,application这四个Scope逐次查找相应的EL表达式所对应的对象的值。那么如果要使用JSTL来读取Action中的变量,就需要把Action中的变量,放到request域中才行。所以,早在Webwork2.1.X的年代,我们会编写一个拦截器来做这个事情的。大致的原理是:在Action执行完返回之前,依次读取Action中的所有的变量,并依次调用request.setAttribute()来进行设置。具体的整合方式,请参考以下这篇文档:http://wiki.opensymphony.com/display/WW/Using+WebWork+and+XWork+with+JSP+2.0+and+JSTL+1.1

不过随着时代的发展,上面的这种方式,已经不再被推荐使用了。(虽然如此,我们依然可以学习它的一个解决问题的思路)目前来说,自从Webwork2.2以后,包括Struts2,都使用另外一种整合方式:对HttpServletRequest进行装饰。让我们来看一下源码:

Java代码  
1.public class StrutsRequestWrapper extends HttpServletRequestWrapper {     
2.    
3.    /**   
4.     * The constructor   
5.     * @param req The request   
6.     */    
7.    public StrutsRequestWrapper(HttpServletRequest req) {      
8.        super(req);      
9.    }     
10.    
11.    /**   
12.     * Gets the object, looking in the value stack if not found   
13.     *   
14.     * @param s The attribute key   
15.     */    
16.    public Object getAttribute(String s) {      
17.        if (s != null && s.startsWith("javax.servlet")) {      
18.            // don't bother with the standard javax.servlet attributes, we can short-circuit this      
19.            // see WW-953 and the forums post linked in that issue for more info      
20.            return super.getAttribute(s);      
21.        }     
22.    
23.        ActionContext ctx = ActionContext.getContext();     
24.        Object attribute = super.getAttribute(s);      
25.    
26.        boolean alreadyIn = false;      
27.        Boolean b = (Boolean) ctx.get("__requestWrapper.getAttribute");      
28.        if (b != null) {      
29.            alreadyIn = b.booleanValue();     
30.        }     
31.    
32.        // note: we don't let # come through or else a request for      
33.        // #attr.foo or #request.foo could cause an endless loop      
34.        if (!alreadyIn && attribute == null && s.indexOf("#") == -1) {      
35.            try {      
36.                // If not found, then try the ValueStack      
37.                ctx.put("__requestWrapper.getAttribute", Boolean.TRUE);      
38.                ValueStack stack = ctx.getValueStack();     
39.                if (stack != null) {      
40.                    attribute = stack.findValue(s);     
41.                }     
42.            } finally {      
43.                ctx.put("__requestWrapper.getAttribute", Boolean.FALSE);      
44.            }     
45.        }     
46.        return attribute;      
47.    }     
48.}   
Java代码 
1.public class StrutsRequestWrapper extends HttpServletRequestWrapper {     
2.    
3.    /**   
4.     * The constructor   
5.     * @param req The request   
6.     */    
7.    public StrutsRequestWrapper(HttpServletRequest req) {     
8.        super(req);     
9.    }     
10.    
11.    /**   
12.     * Gets the object, looking in the value stack if not found   
13.     *   
14.     * @param s The attribute key   
15.     */    
16.    public Object getAttribute(String s) {     
17.        if (s != null && s.startsWith("javax.servlet")) {     
18.            // don't bother with the standard javax.servlet attributes, we can short-circuit this     
19.            // see WW-953 and the forums post linked in that issue for more info     
20.            return super.getAttribute(s);     
21.        }     
22.    
23.        ActionContext ctx = ActionContext.getContext();     
24.        Object attribute = super.getAttribute(s);     
25.    
26.        boolean alreadyIn = false;     
27.        Boolean b = (Boolean) ctx.get("__requestWrapper.getAttribute");     
28.        if (b != null) {     
29.            alreadyIn = b.booleanValue();     
30.        }     
31.    
32.        // note: we don't let # come through or else a request for     
33.        // #attr.foo or #request.foo could cause an endless loop     
34.        if (!alreadyIn && attribute == null && s.indexOf("#") == -1) {     
35.            try {     
36.                // If not found, then try the ValueStack     
37.                ctx.put("__requestWrapper.getAttribute", Boolean.TRUE);     
38.                ValueStack stack = ctx.getValueStack();     
39.                if (stack != null) {     
40.                    attribute = stack.findValue(s);     
41.                }     
42.            } finally {     
43.                ctx.put("__requestWrapper.getAttribute", Boolean.FALSE);     
44.            }     
45.        }     
46.        return attribute;     
47.    }     
48.}   
public class StrutsRequestWrapper extends HttpServletRequestWrapper {  
 
    /** 
     * The constructor 
     * @param req The request 
     */ 
    public StrutsRequestWrapper(HttpServletRequest req) {  
        super(req);  
    }  
 
    /** 
     * Gets the object, looking in the value stack if not found 
     * 
     * @param s The attribute key 
     */ 
    public Object getAttribute(String s) {  
        if (s != null && s.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(s);  
        }  
 
        ActionContext ctx = ActionContext.getContext();  
        Object attribute = super.getAttribute(s);  
 
        boolean alreadyIn = false;  
        Boolean b = (Boolean) ctx.get("__requestWrapper.getAttribute");  
        if (b != null) {  
            alreadyIn = b.booleanValue();  
        }  
 
        // note: we don't let # come through or else a request for  
        // #attr.foo or #request.foo could cause an endless loop  
        if (!alreadyIn && attribute == null && s.indexOf("#") == -1) {  
            try {  
                // If not found, then try the ValueStack  
                ctx.put("__requestWrapper.getAttribute", Boolean.TRUE);  
                ValueStack stack = ctx.getValueStack();  
                if (stack != null) {  
                    attribute = stack.findValue(s);  
                }  
            } finally {  
                ctx.put("__requestWrapper.getAttribute", Boolean.FALSE);  
            }  
        }  
        return attribute;  
    }  



看到了嘛?这个类会在Struts2初始化的时候,替换HttpServletRequest,运行于整个Struts2的运行过程中,当我们试图调用request.getAttribute()的时候,就会执行上面的这个方法。(这是一个典型的装饰器模式)在执行上面的方法时,会首先调用HttpServletRequest中原本的request.getAttribute(),如果没有找到,它会继续到ValueStack中去查找,而action在ValueStack中,所以action中的变量通过OGNL表达式,就能找到对应的值了。

在这里,在el表达式广泛使用的今天,JSTL1.1以后,也支持直接使用el表达式。注意与直接使用struts2的tag的区别,这里需要使用el的表示符号:${}

例如:${user.name}, <c:out value="${department.name}" />

提问:在Struts2中,如何使用Freemarker等模板来读取Action中的变量以及HttpServletRequest和HttpSession中的变量?

Freemarker等模板在Struts2中有对应的Result,而在这些Result中,Freemarker等模板会根据ValueStack和ActionContext中的内容,构造这些模板可识别的Model,从而使得模板可以以他们各自的语法对ValueStack和ActionContext中的内容进行读取。

有关Freemarker对于变量的读取,可以参考Struts2的官方文档,非常详细:http://struts.apache.org/2.0.14/docs/freemarker.html

设值计算

Struts2中使用OGNL进行设值计算,就是指View层传递数据到Control层,并且能够设置到相应的Java对象中。这个过程从逻辑上说需要分成两步来完成:

1. 对于每个请求,都建立一个与相应Action对应的ActionContext作为OGNL的上下文环境和ValueStack,并且把Action压入ValueStack

2. 在请求进入Action代码前,通过某种通用的机制,搜集页面上传递过来的参数,并调用OGNL相关的代码,对Action进行设值。
上面的第一个步骤,在处理URL请求时完成,而第二个步骤由struts2内置的拦截器完成。[/size]
分享到:
评论

相关推荐

    Struts2 & OGNL

    OGNL(Object-Graph Navigation Language)是Struts2中的默认表达式语言,用于在视图和控制器之间传递数据。在这个主题中,我们将深入探讨Struts2与OGNL的整合以及它们在实际开发中的应用。 **Struts2框架概述** ...

    马士兵Struts2笔记2013

    OGNL是一种强大的表达式语言,用于在Struts2中获取和设置对象属性,以及执行复杂的逻辑操作。在视图层,它可以用来动态地渲染页面内容。 14. **Struts投影** 投影是Struts2中将对象属性映射到视图组件的方式,...

    ognl表达式 ognl表达式

    在Struts2中,OGNL的核心组件之一是`ValueStack`,它充当了OGNL表达式的数据源。`ValueStack`是一个存储数据的栈结构,包含了所有的模型对象以及请求、会话和应用范围的数据。开发者可以通过EL表达式直接访问`...

    struts2框架面试题及答案02

    在Struts2中,每个Action实例都有一个与之关联的ValueStack对象,该对象贯穿整个Action的生命周期。ValueStack不仅包含了Action对象本身,还可能包含其他辅助对象或数据,比如表单数据、验证错误等。 #### 获取...

    Struts2框架基础 二

    #### 四、Struts2中的拦截器 拦截器(interceptor)是Struts2的核心特性之一,用于扩展框架的功能。它们可以在执行Action前后进行拦截,并执行相应的操作。 ##### 4.1 拦截器的工作原理 - **注册**:在配置文件中...

    Struts2入门教程(全新完整版)

    1.概述strust2中的拦截器 28 2.自定义拦截器 28 方式一,实现Interceptor接口。 28 方式二、继承AbstractInterceptor抽象类 29 方式三、继承MethodFilterInteceptor类 30 3.使用来MethodFilterInterceptor灵活拦截 ...

    struts2的学习笔记+测试源代码

    这篇学习笔记和测试源代码将深入探讨Struts2中的类型转换机制。 首先,了解为什么类型转换是必要的。在Web应用中,用户通过HTML表单提交的数据通常是字符串类型。然而,后台业务逻辑可能需要这些数据以其他类型存在...

    JavaEE ActionContext存取数据示例

    ValueStack是Struts2中的核心概念,它是一个对象栈,用于存储Action实例和模型对象。通过OGNL表达式,我们可以方便地从ValueStack中获取或设置数据。OGNL是一种强大的表达式语言,用于获取和设置对象属性,支持动态...

    精讲Struts2

    精简全面讲解Struts 2.0Struts2产生背景 工作原理 实现步骤 标签 命名空间 转换器 国际化 输出 令牌机制 拦截器 表单校验(jQuery.validate验证框架) ...OGNL(ActionContext StackContext ValueStack区别)

    详解Java的Struts框架中栈值和OGNL的使用

    在Struts 2中,ActionContext类提供了对值栈的访问,可以通过`ActionContext.getContext().getValueStack()`获取值栈。一旦获取到值栈,就可以通过栈的各种操作(如push、pop)来添加或移除对象,以及通过OGNL表达式...

    如何自定义Struts2表单验证后的错误信息显示格式/样式_第二话

    **标签介绍**:`s:debug`标签主要用于辅助测试,可以在页面上生成一个超链接,通过点击此链接可以查看`ValueStack`和`StackContext`中的所有值信息。 **示例代码**: ```html Display Error Info for Struts2...

    Struts2源码分析之ParametersInterceptor拦截器

    ValueStack是OGNL(Object-Graph Navigation Language)表达式语言在Struts2中的核心组件,它管理着应用程序的模型数据。 接下来,关键的方法setParameters(action, stack, parameters)被调用,它负责将参数设置到...

    ONGL中(#.%.$)三种符号的用法.pdf

    首先,`#`符号在OGNL中的主要作用是访问Stack Context中的值,以及创建和访问特定的命名对象。例如,`#parameters`对象可以用来获取HTTP请求参数,`#request`用于访问HttpServletRequest的属性,`#session`用于访问...

Global site tag (gtag.js) - Google Analytics