zoukankan      html  css  js  c++  java
  • Struts2学习笔记四 OGNL

    OGNL,全称为Object-Graph Navigation Language(对象图表达语言),它是一个功能强大的表达式语言,用来获取和设置Java对象的属性,调用java对象的方法,同时能够自动实现必要的类型转换。它旨在提供一个更高的更抽象的层次来对Java对象图进行导航。如果把表达式看作是一个带有语义的字符串,那么OGNL无疑成为了这个语义字符串与Java对象之间沟通的桥梁。

    OGNL:对象视图导航语言. ${user.addr.name} 这种写法就叫对象视图导航.
    OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能.

    OGNL的作用

    Struts默认的表达式语言就是OGNL,它具有以下特点:

    • 支持对象方法调用。
    • 支持类静态方法调用和值访问,表达式的格式为@[类全名(包括包路径)]@[方法名|值名],例如:@java.lang.String@format('foo%s','bar')。
    • 支持赋值操作和表达式串联。例如:price=100,discount=0.8,calculatePrice(),在方法中进行乘法计算会返回80.
    • 访问OGNL上下文(OGNL context)和ActionContext。
    • 操作集合对象。

    OGNL的要素

    1、表达式

    表达式是整个OGNL的核心,OGNL会根据表达式去对象中取值。所有OGNL操作都是针对表达式解析后进行的。它表明了此次OGNL操作要“做什么”。表达式就是一个带有语法含义的字符串,这个字符串规定了操作的类型和操作的内容。OGNL支持大量的表达式语法,不仅支持这种“链式”对象访问路径,还支持在表达式中进行简单的计算。

    2、根(root)对象

    Root对象可以理解为OGNL的操作对象,表达式规定了“做什么”,而Root对象则规定了“对谁操作”。OGNL称为对象图导航语言,所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个对象关联的其他对象。

    3、上下文(context)对象

    实际上OGNL的取值还需要一个上下文环境。设置了Root对象,OGNL可以对Root对象进行取值或写值等操作,Root对象所在环境就是OGNL的上下文环境。上下文环境规定了OGNL的操作“在哪里进行”。上下文环境Context是一个Map类型的对象,在表达式中访问Context中的对象,需要使用"#"号加上对象名称,即“#对象名称”的形式。

    OGNL的入门

    struts2 的包中已经包含了,所以不需要导入额外的jar包。

    OGNL取值

        @Test
        //基本语法演示
        //取出context中的属性值
        public void fun3() throws Exception{
            //准备ONGLContext
                //准备Root
                User rootUser = new User("tom",18);
                //准备Context
                Map<String,User> context = new HashMap<String,User>();
                context.put("user1", new User("jack",18));
                context.put("user2", new User("rose",22));
            OgnlContext oc = new OgnlContext();
            oc.setRoot(rootUser);
            oc.setValues(context);
            //书写OGNL
            //取出root中user对象的name属性
            String name = (String) Ognl.getValue("name", oc, oc.getRoot());
            Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
            System.out.println(name);
            System.out.println(age);
            //取出context中键为user1对象的name属性
            String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
            String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
            Integer age = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());
            System.out.println(name);
            System.out.println(name2);
            System.out.println(age);
        }

    对于使用上下文的OGNL,若不指定从哪一个对象中查找"name"属性,则OGNL直接从根对象(root)查找,若指定查找对象(使用'#'号指定,如#user1),则从指定的对象中查找,若指定对象不在上下文中则会抛出异常,即使用#user1.name形式指定查找对象则必须要保证指定对象在上下文环境中。

    OGNL为属性赋值

        @Test
        //基本语法演示
        //为属性赋值
        public void fun4() throws Exception{
            //准备ONGLContext
            //准备Root
            User rootUser = new User("tom",18);
            //准备Context
            Map<String,User> context = new HashMap<String,User>();
            context.put("user1", new User("jack",18));
            context.put("user2", new User("rose",22));
            OgnlContext oc = new OgnlContext();
            oc.setRoot(rootUser);
            oc.setValues(context);
            //书写OGNL
            //将root中的user对象的name属性赋值
            Ognl.getValue("name='jerry'", oc, oc.getRoot());
            String name = (String) Ognl.getValue("name", oc, oc.getRoot());
            //把user1的name改为xq,并取值,如果多个取值放在一起,只会返回最后一个表达式的值
            String name2 = (String) Ognl.getValue("#user1.name='xq',#user1.name", oc, oc.getRoot());
            System.out.println(name);
            System.out.println(name2);
        }

    OGNL调用方法

        @Test
        //基本语法演示
        //调用方法
        public void fun5() throws Exception{
            //准备ONGLContext
            //准备Root
            User rootUser = new User("tom",18);
            //准备Context
            Map<String,User> context = new HashMap<String,User>();
            context.put("user1", new User("jack",18));
            context.put("user2", new User("rose",22));
            OgnlContext oc = new OgnlContext();
            oc.setRoot(rootUser);
            oc.setValues(context);
            //书写OGNL
    
            //调用root中user对象的setName方法
            Ognl.getValue("setName('lilei')", oc, oc.getRoot());
            String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());
    
            String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());
            System.out.println(name);
            System.out.println(name2);
        }

    OGNL调用静态方法

        @Test
        //基本语法演示
        //调用静态方法
        public void fun6() throws Exception{
            //准备ONGLContext
            //准备Root
            User rootUser = new User("tom",18);
            //准备Context
            Map<String,User> context = new HashMap<String,User>();
            context.put("user1", new User("jack",18));
            context.put("user2", new User("rose",22));
            OgnlContext oc = new OgnlContext();
            oc.setRoot(rootUser);
            oc.setValues(context);
            //书写OGNL
    
            String name = (String) Ognl.getValue("@java.lang.String@format('hello %s!','world')", oc, oc.getRoot());
            //Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
            Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
            System.out.println(name);
            System.out.println(pi);
        }

    创建list对象

        @Test
        //基本语法演示
        //ognl创建对象-list|map
        public void fun7() throws Exception{
            //准备ONGLContext
            //准备Root
            User rootUser = new User("tom",18);
            //准备Context
            Map<String,User> context = new HashMap<String,User>();
            context.put("user1", new User("jack",18));
            context.put("user2", new User("rose",22));
            OgnlContext oc = new OgnlContext();
            oc.setRoot(rootUser);
            oc.setValues(context);
            //书写OGNL
            
            //创建list对象
            Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
            String name = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());
            String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());
        
            System.out.println(size);
            System.out.println(name);
            System.out.println(name2);
            //创建Map对象(#表示要创建一个map)
            Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());
            String name3  = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());
            Integer age  = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());
            System.out.println(size2);
            System.out.println(name3);
            System.out.println(age);
        }    

    OGNL与Struts2结合 

    什么是值栈?

    具体参考这里,ValueStack是Struts的一个接口,OGNLValueStack是ValueStack的实现类,客户端发起一个请求Struts2架构会创建一个action实例,同时创建一个OgnlValueStack值栈实例,OgnlValueStack贯穿整个Action的生命周期,Struts2中使用OGNL将请求Action的参数封装为对象存储到值栈中,并通过OGNL表达式读取值栈中的对象属性值。

        CompoundRoot root;
        transient Map<String, Object> context;

    Context:即OgnlContext上下文,它是一个map结构,上下文中存储了一些引用,parameters、request、session、application等,上下文的Root为CompoundRoot。

    OgnlContext中的一些引用:

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

    CompoundRoot:存储了action实例,它作为OgnlContext的Root对象。action实例位于栈顶,当读取action的属性值时会先从栈顶对象中找对应的属性,如果找不到则继续找栈中的其他对象,如果找到则停止查找。

    CompoundRoot继承了ArrayList实现压栈和出栈功能,拥有栈的特点,先进后出,最后压进栈的数据在栈顶。我们把它称为对象栈。

    Struts2对原OGNL做出的改进就是Root使用CompoundRoot(自定义栈),使用OgnlValueStack的findValue方法可以在CompoundRoot中从栈顶向栈底找查找的对象的属性值。

     ActionContext和ValueStack的关系

    PrepareOperations源码:
    public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
            Integer counter = Integer.valueOf(1);
            Integer oldCounter = (Integer)request.getAttribute("__cleanup_recursion_counter");
            if(oldCounter != null) {
                counter = Integer.valueOf(oldCounter.intValue() + 1);
            }
    
            ActionContext oldContext = ActionContext.getContext();
            ActionContext ctx;
            if(oldContext != null) {
                ctx = new ActionContext(new HashMap(oldContext.getContextMap()));
            } else {
                ValueStack stack = ((ValueStackFactory)this.dispatcher.getContainer().getInstance(ValueStackFactory.class)).createValueStack();
                stack.getContext().putAll(this.dispatcher.createContextMap(request, response, (ActionMapping)null));
                ctx = new ActionContext(stack.getContext());
            }
    
            request.setAttribute("__cleanup_recursion_counter", counter);
            ActionContext.setContext(ctx);
            return ctx;
        }

    从上源码可以看出:

    在创建ActionContext的时候,创建ValueStack的对象,将ValueStack对象给ActionContext。

     ActionContext中有一个ValueStack的引用。ValueStack中也有一个ActionContext的引用。

    ActionContext获取Servlet API的时候,就依赖值栈。

     获取值栈对象

    1、通过ActionContext对象获取值栈

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

    2、通过request域获取值栈

    ValueStack stack2= (ValueStack)ServletActionContext.getRequest().getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);

    查看值栈中两部分内容(使用DEBUG标签)

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <!-- 调试标签 -->
    <s:debug></s:debug>
    </body>
    </html>

    struts2与ognl结合体现—参数接收

    必须在params拦截器之前,把对象压入栈顶。可以使用parpre拦截器,只需在action中实现Preparable接口即可。具体参考这里

    public class Demo2Action implements Preparable {
        private User u = new User();
    
        @Override
        public String execute() throws Exception {
            System.out.println(u);
            return SUCCESS;
        }
    
        @Override
        public void prepare() throws Exception {
            //压入栈顶
            //1获得值栈
            ValueStack vs = ActionContext.getContext().getValueStack();
            //2将u压入栈顶
            vs.push(u);
        }
    }

    struts2与ognl结合体现—配置文件

    import com.opensymphony.xwork2.ActionSupport;
    
    public class Demo3Action extends ActionSupport {
        
        private String  name;
        
        @Override
        public String execute() throws Exception {
            name = "jerry";
            return SUCCESS;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    <action name="Demo3Action" class="cn.itheima.d_config.Demo3Action" method="execute" >
                <result name="success" type="redirectAction" >
                    <param name="actionName">Demo1Action</param>
                    <param name="namespace">/</param>
                    <!-- 如果添加的参数struts"看不懂".就会作为参数附加重定向的路径之后.
                         如果参数是动态的.可以使用${}包裹ognl表达式.动态取值
                     -->
                    <param name="name">${name}</param>
                </result>
            </action>

     扩展:request对象的getAttribute方法,查找顺序

  • 相关阅读:
    博客园的界面设置
    ARM 汇编指令集
    winfroms更换皮肤
    面向对象的七项设计原则
    S2-01
    机票查询与订购系统
    重点语法
    第二章
    一、17.09.13
    习作
  • 原文地址:https://www.cnblogs.com/ginb/p/7300503.html
Copyright © 2011-2022 走看看