struts2框架学习笔记5: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类:

struts2框架学习笔记5:OGNL表达式struts2框架学习笔记5:OGNL表达式
package bean;

public class User {
    private String name;
    private Integer age;
    
    
    
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    public User(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    
}
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());

相关推荐