获取ServletAPI:

  第一种方式:

 //在request域中放入属性req,暂且认为getContext()获取的是request域空间,但实际不是
ActionContext.getContext().put("req", "req_value");
//在session域中放入属性ses
ActionContext.getContext().getSession().put("ses", "ses_value");
//在application域中放入属性app
ActionContext.getContext().getApplication().put("app", "app_value");

  第二种方式:

1 //获取真正的HttpServletRequest
HttpServletRequest request = ServletActionContext.getRequest();
//向request中放入属性
request.setAttribute("req", "req_value");
//向session中放入属性
request.getSession().setAttribute("ses", "ses_value");
//向application中放入属性
ServletActionContext.getServletContext().setAttribute("app", "app_value");

  第三种方式(不推荐):

   public class LoginAction implements RequestAware, SessionAware, ApplicationAware 实现这三个接口

 package com.tongji.actions;

 import java.util.Map;

 import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware; public class LoginAction implements RequestAware, SessionAware, ApplicationAware{
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application; public String execute() {
request.put("req", "req_value");
session.put("ses", "ses_value");
application.put("app", "app_value"); return "success";
} @Override
public void setApplication(Map<String, Object> application) {
this.application = application;
} @Override
public void setSession(Map<String, Object> session) {
this.session = session;
} @Override
public void setRequest(Map<String, Object> request) {
this.request = request;
} }

  综述:第一种方式简单,适合只向对应域中放值;第二种方式复杂,但是能获取更多对应域中的属性;第三种不推荐,因为实现接口,容易导致注入式编程。

OGNL与值栈:

  1. 简介:

    OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言,是一个第三方开源项目。

    Struts2通过使用OGNL简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转换等功能。

    使用OGNL,要导入struts的标签:<%@ taglib uri="/struts-tags" prefix="s"%>

  2. 特点:

    相对其他表达式语言,它提供了丰富的功能:

      (1)支持对象方法调用,如xxx.sayHello()

      (2)支持类静态方法调用和常量访问,表达式的格式为:@java.lang.Math@PI 或 @java.lang.Math@random()

        不过,对于静态方法的访问,需要通过在struts.xml中修改常量struts.ognl.allowStaticMethodAccess为true进行开启。

      (3)可以操作集合对象

      (4)可以直接创建对象

  3. 详细介绍:    

      The framework uses a standard naming context to evaluate OGNL expressions. The top level object dealing with OGNL is a Map (usually referred as a context map or context).

    OGNL has a notion of there being a root (or default) object within the context. In expression, the properties of the root object can be referenced without any special "marker" notion.

    References to other objects are marked with a pound sign (#).

      The framework sets the OGNL context to be our ActionContext, and the value stack to be the OGNL root object. (The value stack is a set of several objects, but to OGNL it appears to be a single object.)

    Along with the value stack, the framework places other objects in the ActionContext, including Maps representing the application, session, and request contexts. These objects coexist in the ActionContext,

    alongside the value stack (our OGNL root).

      aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxQAAAE2CAYAAAD4eNSFAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAACAUSURBVHhe7d3rmao6FAbgKciCbMdmqMVyTgceA6JcAkS8zMrs98f77D0SQwKK6wOc+fnvv/8uAAAAewgUAADAbgIFAACwm0ABAADsJlAAAAC7CRQAAMBuAgUAALCbQAEAAOwmUAAAALsJFAAAwG4CBQAAsJtAAQAA7CZQEFRzOZ3OmccBAIhEoCAogQIAoAYCBUEJFAAANRAoCEqgAACogUBBUAIFAEANBAqCEigAAGogUBCUQAEAUAOBgqAECgCAGggUBCVQAADUQKAgKIECAKAGAgVBCRQAADUQKAhKoAAAqIFAQVACBQBADQQKghIoAABqIFAQlEABAFADgYKgBAoAgBoIFAQlUAAA1ECgAAAAdhMoAACA3QQKAABgN4GCoHyHAgCgBgIFQQkUAAA1ECgISqAAAKiBQEFQfzVQNJfj4XQ5Z5cBANRHoCCoPxYozqfL4efn8pNsBIrz6dC2Ozb55e90Pp0uTebxoW+OBwCoj0BBUH/0CkUKFoGuUDTH42agAABYI1AQlEDxce1VE4ECAHiNQEFQ3wgUzeXY34bUOlxO58fy/lafw3UczfHRbnjrT0mbkbVAUXpbVHO8r2c65s76vObLb47NoM1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rHM+ljOran+tkw337T93vXpuS4sdVmPKfeZLy79vvW6zA/r7uF/dX65uuwwHg9ndz7dGv7zNvMx5GWb+7TZOW1mhSN+dpHbh4RCBQE9aUvZf8F6UC+cJ8l+3QfEK++/tIH8/xDY9+9r/lwEkeaa+TxJdG3YQ1swzUlx433HFuI5Gv7NPhnvUBBUAIFv2N4lujlD4nMWbvn+0xF3MIZrwhGc4xabAbfhlWwDdeUHDfeemwhBPv0QaAAAAB2EygAAIDdBAqCcssTAEANBAqCEigAAGogUBCUQAEAUAOBgqAECgCAGggUBCVQAADUQKAgKIECAKAGAgVBCRQAADUQKAhKoAAAqIFAQVACBQBADQQKghIoAABqIFAQlEABAFADgYKgBAoAgBoIFAQlUAAA1ECgICiBAgCgBgIFQQkUAAA1ECgISqAAAKiBQEFQAgUAQA0ECgAAYDeBAgAA2E2gAAAAdhMoAACA3QQKAABgN4ECAADYTaAAAAB2EygAAICd/rv8D1NyUt1HSBECAAAAAElFTkSuQmCC" alt="" />

   4. 值栈的创建:

      在用户提交一个Action请求后,系统会马上创建两个对象:Action实例与值栈对象。Struts2中的值栈ValueStack是个接口,其实现类为OgnlValueStack。

      OgnlValueStack类中包含两个关键的成员变量:root 和 context。

      (1)root:

        root的底层是一个封装了ArrayList的栈,对值栈ValueStack的访问,就是对root中对象的访问。

      (2)context:

        context的类型为Map<String,Object>,在创建ValueStack时,就初始化root,在初始化root时,就创建了一个默认的context,而这个默认的context(Map)中又立即存放了这个ValueStack(其实是地址),

        所以会出现context是ValueStack的成员变量,但同时ValueStack又属于context,这种情况。

    5. 值栈的获取:

      (1)麻烦的方法:

         当一个Action请求到来时,不仅会创建一个Action实例,还会创建一个ValueStack对象,用于存放Action运行过程中的相关数据。所以Action实例和ValueStack对象的生命周期是相同的,为了保证这一点,就将ValueStack通过setAttribute()方法,将其放入到了Request的域属性中,并将该属性的key以常量的形式保存在ServletActionContext中。所以,有了以下获取ValueStack的方法:

         ValueStack valueStack1 =  (ValueStack) ServletActionContext.getRequest().getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
      (2)简单的方法:

          由于ValueStack中的context需要经常访问,所以Struts2为这个context取了个别名ActionContext,所以。又有了以下获取ValueStack的方法:

         ValueStack valueStack2 = ActionContext.getContext().getValueStack();

    6. 值栈的操作:

      对于值栈的操作分为向值栈中放入数据和从值栈中读取数据。向值栈中放入数据分为显示放入和隐式放入。

      (1)向root中显示放入数据:

 Student student1 = new Student("张三",23);
Student student2 = new Student("李四",24);
Student student3 = new Student("王五",25);
Student student4 = new Student("赵六",26);
Student student5 = new Student("阮七",27);
//将对象直接放入值栈(无名对象)
ActionContext.getContext().getValueStack().push(student1);
//将对象直接放入值栈的root(无名对象)
ActionContext.getContext().getValueStack().getRoot().push(student2);
//向值栈中放入有名对象:通过map
Map<String,Object> map = new HashMap<String,Object>();
map.put("student3", student3);
ActionContext.getContext().getValueStack().push(map);
//使用值栈的set方法放入有名对象
ActionContext.getContext().getValueStack().set("student4", student4);
//将root当作ArrayList,向其中添加对象(无名对象)
ActionContext.getContext().getValueStack().getRoot().add(student5);
ActionContext.getContext().getValueStack().getRoot().add(0,student5); 

     前端获取数据时:

      name = ${name}<br>
        name = <s:property value="name"/><br>
        age = ${age}<br>
        age = <s:property value="age"/><br>
        student3 = <s:property value="student3"/>
        student4 = <s:property value="student4"/>

     综述:由于root的底层是ArrayList类型,向值栈中直接放入对象时,只有索引,没有key,也就没有名称,所以这种方式叫做放入无名对象。这种情况,在获取某一个属性,比如name时,即name = <s:property value="name"/><br>,它只会提取栈顶对象的name属性;

        要想向值栈中放入有名对象,就要通过map来实现,这又可以分成两种方式:

          (1)手动创建map对象,向其中添加对象和对象名,再将这个map放入值栈中,这种方式创建一个map对象,就会在值栈找存在一个map。

          (2)使用值栈的set方法,放入对象和对象名,这种方式首先判断值栈中是否已经存在由set创建的map(不考虑手动创建的map)。如果没有创建,则自动创建一个map,并向其中放入值;如果已经有了map,则直接在该map放入值。

        这种情况,可以通过对象名称获取相应对象,即student3 = <s:property value="student3"/>,进一步可以获取这个对象中的属性,即student3-name = <s:property value="student3.name"/><br>

 调试结构图(通过添加<s:debug />就会显示):

        

        注:与代码不对应,但从其中可以发现,从值栈中获取数据有两种情况,一种是栈顶属性法,一种是map中带有有名对象法。

      (2)向root中隐式放入数据:

        在值栈创建好之后,首先将创建好的XXXAction对象直接放入到值栈的栈顶。之后,前端表单提交的数据隐式地传入相应XXXAction的空间中。

        

         上图中Action接收请求参数为属性驱动方式

        

         上图中Action接收请求参数为域驱动方式,这种方式,想获取name和age属性,就要:name = ${student.name} 或 name = <s:property value="student.name"/>。

         但要注意:这种域驱动方式中接收数据的对象的名称,不能和前面中map中有名对象的名称相同,不然会被覆盖。也就是说,set出来的map或手动创建的map中的有名对象名,会覆盖域驱动方式中的对象名。

      (3)向context中显示放入数据:

 Student student1 = new Student("张三",23);
//将数据直接放入context
ActionContext.getContext().put("student1", student1);
//向session与application中放入数据
ActionContext.getContext().getApplication().put("other", "app_value");
ActionContext.getContext().getSession().put("some", "ses_value");
ActionContext.getContext().getApplication().put("some", "app_value"

        

         上图为context中的一项。

        前端获取数据时:

         student1 = <s:property value="student1"/><br>
            #session.some = <s:property value="#session.some"/><br>
            #application.some = <s:property value="#application.other"/><br>
            #attr.some = <s:property value="#attr.some"/><br>
            #attr.other = <s:property value="#attr.other"/><br>

         其中attr是按照如下的顺序依次寻找相应属性:

        aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAocAAAArCAYAAAAKVSJeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAf3SURBVHhe7dxhmuoqDIDhWZALcjtupmtxObODnoZapkAgaYueqt+P93nusUhDIBSdmfvz+/s7/hnG2+2++jcAAAC+CYdDAAAARBwOAQAAEHE4BAAAQMThEAAAAFF2OAQAAMA343AIAACAiMMhAAAAIn7nEAAAABGHw73ut/FyHfRrAADg/X3ps57D4S7DeL3cxrt6DQAAfIzhOl6+7G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VxRZX5Cl65Dh3S+8y0OrXyU7Yp45Dr5pyKxloVrpinPrRxfLL/8wcpn0CKsvJ7CdhnLvaj60822XID2Pe7IvpB8zxkrGeOT5w9h++AHLZ49o0+ewvO5GVz+qXPeg6HOIX1p7fDBa98mt7epzyQK59EzyAZ41kPDifP4Vsghy2efaPr3oJTYE6fLzscAgAA4JtxOAQAAEDEj5UBAAAQcTgEAABAxOEQAAAAEYdDAAAARBwOAQAAEHE4BAAAQMThEAAAABGHQwAAAEQcDgEAABBxOAQAAEDE4RAAAAARh0MAAABEHA4BAAAQcTgEAABAxOEQAAAAEYdDAAAAPPyO/wBLPIF4C2JbCQAAAABJRU5ErkJggg==" alt="" />

      (4)向context中隐式放入数据:

        前端表单提交的数据,会隐式地传到context中的parameters中,无论XXXAction中是否存在相应的属性。

        另外,创建好的XXXAction对象不仅会放入值栈的栈顶,还会放入context中的action中。

        

        前端获取数据时:

        #parameters.name = <s:property value="#parameters.name"/><br>
            #parameters.age = <s:property value="#parameters.age"/><br>
            name = <s:property value="name"/><br>
            age = <s:property value="age"/><br>
            #action.name = <s:property value="#action.name"/><br>
            #action.age = <s:property value="#action.age"/><br>

      (5)root中数据的加载顺序:

        ActionContext.getContext().put("some", "context_value");
             ActionContext.getContext().getValueStack().set("some", "root_value");

         前端获取数据,在获取不带#号的数据时,首先查找值栈中是否存在该数据,如果不存在该数据,则从context中查找,如果有,就返回context中的值。

        some = <s:property value="some"/>

        (6)request中数据的加载顺序:

        ActionContext.getContext().put("some", "context_value");
             ActionContext.getContext().getValueStack().set("some", "root_value");
               ServletActionContext.getRequest().setAttribute("some", "req_value");
        前端获取数据,在获取带#request的请求数据时,依次查找请求中、值栈中和context中是否有值,有值就返回。

        #request.some = <s:property value="#request.some"/>

        这就解释了,这个问题。(页面最上方==》暂且认为getContext()获取的是request域空间,但实际不是)

    7. OGNL对于集合的操作(主要包括:List与Map的创建和遍历,集合元素的判断,集合投影,集合过滤):

   <br>--------List--------<br>
<!-- 通过set标签定义的变量,会放到context -->
<s:set name="names" value="{'zs','ls','ww'}"/>
<s:iterator value="#names"> <!-- 会将当前迭代的对象自动放入到值栈的栈顶 -->
<s:property/><br> <!-- 若s:property不给出value属性值,则会自动将值栈栈顶元素输出 -->
</s:iterator>
<br>--------Map--------<br>
<s:set name="maps" value="#{'mobile':'1234567','QQ':'7654321'}"/>
<s:iterator value="#maps">
<s:property/><br>
</s:iterator>
<s:iterator value="#maps">
<s:property value="entry"/><br>
</s:iterator>
<s:iterator value="#maps">
<s:property value="key"/> = <s:property value="value"/><br>
</s:iterator>
<br>--------in与not in--------<br>
<s:property value="'zs' in #names"/><br>
<s:property value="'zs' not in #names"/><br>
<br>--------创建三个Student对象--------<br>
<s:bean name="com.tongji.beans.Student" id="student3"> <!-- 该student会放入context中 -->
<s:param name="name" value="'张三'" />
<s:param name="age" value="23" />
</s:bean>
<s:bean name="com.tongji.beans.Student" id="student4"> <!-- 该student会放入context中 -->
<s:param name="name" value="'李四'" />
<s:param name="age" value="24" />
</s:bean>
<s:bean name="com.tongji.beans.Student" id="student5"> <!-- 该student会放入context中 -->
<s:param name="name" value="'王五'" />
<s:param name="age" value="25" />
</s:bean>
<br>--------将这三个Student对象定义为一个List--------<br>
<s:set name="students" value="{#student3,#student4,#student5}"/>
<br>--------将这三个Student对象的name定义为一个List,即投影--------<br>
<s:set name="studentNames" value="students.{name}"/>
<s:iterator value="#studentNames">
<s:property/><br>
</s:iterator>
<br>--------查询:age大于23的所有学生--------<br>
<s:iterator value="#students.{?#this.age>23}">
<s:property/><br>
</s:iterator>
<br>--------查询:age大于23的第一个学生--------<br>
<s:iterator value="#students.{^#this.age>23}">
<s:property/><br>
</s:iterator>
<br>--------查询:age大于23的最后一个学生--------<br>
<s:iterator value="#students.{$#this.age>23}">
<s:property/><br>
</s:iterator>

Struts2笔记3--获取ServletAPI和OGNL与值栈的更多相关文章

  1. Struts2 (三) — OGNL与值栈

    一.OGNL表达式 1.概述 1.1什么是OGNL ​ OGNL是Object-Graph Navigation Language的缩写,俗称对象图导航语言. 它是一种功能强大的表达式语言,通过它简单 ...

  2. struts2中,OGNL访问值栈的时候查找的顺序是什么?请排序:模型对象、临时对象、固定名称的对象、Action对象

    struts2中,OGNL访问值栈的时候查找的顺序是什么?请排序:模型对象.临时对象.固定名称的对象.Action对象 解答:struts2的值栈排列顺序为:1).临时对象:2).模型对象:3).Ac ...

  3. [转]Struts2数据传输的背后机制:ValueStack(值栈)

    1. 数据传输背后机制:ValueStack(值栈) 在这一切的背后,是因为有了ValueStack(值栈)! 2. ValueStack基础:OGNL 要了解ValueStack,必须先理解OGNL ...

  4. (转)OGNL与值栈

    http://blog.csdn.net/yerenyuan_pku/article/details/67709693 OGNL的概述 什么是OGNL 据度娘所说: OGNL是Object-Graph ...

  5. struts2学习笔记--总结获取servletAPI的几种方式

    struts2的Action放弃了request,response等ServletAPI,使得在业务层上更加独立,在有时候使用struts2进行Web开发的时候,不可避免的要在action中使用ser ...

  6. 框架学习之Struts2(三)---OGNL和值栈

    一.OGNL概述 1.1OGNL是对象图导航语言(Object-Graph Navigation Languaged)的缩写,他是一种功能强大的表达式语言,通过简单一致的表达式语法,可以存取Java对 ...

  7. 4、OGNL与值栈

    一.OGNL 1.什么是OGNL 对象导航图语言(Object Graph Navigation Language),简称OGNL,是应用于Java中的一个开源的表达式语言(Expression La ...

  8. Struts2数据传输的背后机制:ValueStack(值栈)

    1.     数据传输背后机制:ValueStack(值栈) 在这一切的背后,是因为有了ValueStack(值栈)! ValueStack基础:OGNL 要了解ValueStack,必须先理解OGN ...

  9. EL与OGNL以及值栈的理解

    这里先添加下在项目遇到的问题: 这两天在做论坛项目的时候,犯了一个错误:将数据放入值栈中,结果jsp页面获取不到. 困扰了许久: 总结如下: (1)每个action对应相应页面的值栈中值的获取,在属于 ...

随机推荐

  1. .net mvc C#生成网页快照

    目标:调用某一网页,自动抓取整个页面为图片,并保存 public class WebSiteThumbnail { Bitmap m_Bitmap; string m_Url; public WebS ...

  2. iOS BCD码、数据流、字节和MD5计算

    一.各个之间的相互转换 1.字符串转数据流NSData NSString *str = @"abc123"; NSData *dd = [str dataUsingEncoding ...

  3. BZOJ2958 序列染色(动态规划)

    令f[i][0/1/2][0/1]表示前i位,不存在满足要求的B串和W串/存在满足要求的B串不存在W串/存在满足要求的B串和W串,第i位填的是B/W的方案数.转移时考虑连续的一段填什么.大讨论一波后瞎 ...

  4. 如何合理的规划jvm性能调优

    JVM性能调优涉及到方方面面的取舍,往往是牵一发而动全身,需要全盘考虑各方面的影响.但也有一些基础的理论和原则,理解这些理论并遵循这些原则会让你的性能调优任务将会更加轻松.为了更好的理解本篇所介绍的内 ...

  5. 51nod 1421 最大MOD值 | 暴力

    题面 有一个a数组,里面有n个整数.现在要从中找到两个数字(可以是同一个) ai,aj ,使得 ai mod aj 最大并且 ai ≥ aj. Input 单组测试数据. 第一行包含一个整数n,表示数 ...

  6. shell实践(一)---判断远程服务器中文件是否存在

    1.判断一个文件的常见形式为 if [ -f filename ]  #此处有-e和-f的区别 注意: 1)中括号之间的空格: 2)filename最好是绝对路径,在判断远程服务器中文件是否存在时尤为 ...

  7. Linux内核分析实验二:mykernel实验指导(操作系统是如何工作的)

    计算机是如何工作的?(总结)——三个法宝 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: 函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算机来说并不那么重要 ...

  8. 正确理解 LEAL (Load Effective Address) 指令

    LEAL: leal S, D    ->    D ← &S 在 CSAPP (Computer Systems: A Programmer’s Perspective) 中,对 LE ...

  9. 解题:WC 2007 石头剪刀布

    题面 要我们把边定向,最大化留下来的三元环数目......并不能直接做,考虑容斥,去掉不合法的数目. 那么三个点不成环当且仅当有一个点出度为2一个点入度为2,发现最终答案就是$C_n^3-\sum C ...

  10. bzoj 1814: Ural 1519 Formula 1 插头dp经典题

    用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. #include<iostream> #include<cstdio> #include<cstr ...