码迷,mamicode.com
首页 > 其他好文 > 详细

Struts--课程笔记3

时间:2016-12-28 23:31:22      阅读:426      评论:0      收藏:0      [点我收藏+]

标签:scp   svn   gcs   属性驱动   dac   名称   can   ftp   value   

获取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");

  第二种方式:

   //获取真正的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

   实现这三个接口

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

 

 

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).

      技术分享

   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是按照如下的顺序依次寻找相应属性:

        技术分享

      (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>

Struts--课程笔记3

标签:scp   svn   gcs   属性驱动   dac   名称   can   ftp   value   

原文地址:http://www.cnblogs.com/qjjazry/p/6230630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!