java学习笔记-- struts2框架:OGNL表达式

OGNL取值范围分两部分,root、Context两部分

可以放置任何对象作为ROOT,CONTEXT中必须是Map键值对

示例:

准备工作:

复制代码
    public void fun1() 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();
        // 将rootUser作为root部分
        oc.setRoot(rootUser);
        // 将context这个Map作为Context部分
        oc.setValues(context);
        // 书写OGNL
        Ognl.getValue("", oc, oc.getRoot());
        //在""中书写OGNL表达式即可
    }
复制代码

User类:

  View Code

 

语法:

从root中取出对象:

        // 取出root中user对象的name属性
        String name = (String) Ognl.getValue("name", oc, oc.getRoot());
        Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());

 

从Context中取出对象:

        // 取出context中键为user1对象的name属性
        String name1 = (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());

 

为属性赋值:

        // 将root中的user对象的name属性赋值
        Ognl.getValue("name='jerry'", oc, oc.getRoot());

给context赋值:

        String name2 = (String) Ognl.getValue("#user1.name='张三'", oc, oc.getRoot());

 

调用对象的方法:

复制代码
        // 调用root中user对象的setName方法
        Ognl.getValue("setName('张三')", oc, oc.getRoot());
        String name1 = (String) Ognl.getValue("getName()", oc, oc.getRoot());

        String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());
复制代码

(注意:可以将两条语句写在一起,逗号分隔,返回值是最后一个语句)

 

调用静态方法:

        Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc,oc.getRoot());

 

创建对象:

复制代码
        // 创建list对象
        Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
        String name1 = (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());

        // 创建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());
复制代码

 

 OGNL表达式和Struts2结合原理:

OGNLContext本质上就是Struts2中的值栈(ValueStack

ValueStack中有两部分:ROOT、CONTEXT

(队列:先进先出、栈:先进后出)

ROOT部分是一个栈,CONTEXT部分是ActionContext

ROOT中的这个栈本质是一个容器(集合)

例如用list集合制作栈结构容器:push压栈方法  list.add(0,a);、pop弹栈方法  list.remove(0),就可以模拟一个栈

 

Struts2中有一个类:CompoundRoot类就是一个栈,实现原理和上面类似

用栈的原因:在取栈中的属性时候,会从栈顶开始找属性,找不到继续向下找,找到停止

默认情况下栈中放置的是当前访问的Action对象

 

OGNL表达式和Struts2结合的体现:

Struts2有一个拦截器params,作用是将获得的参数交给OGNL处理

学习java到知海匠库互联网学院

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值