struts2 Ognl

2019-02-11  本文已影响76人  Big_Monster

什么是Ognl

1、存取对象的任意属性,简单说就是对javabean进行操作(重要)
2、调用对象方法。
3、调用类的静态方法
4、索引数组元素
5、操作集合(重 要)

Ognl操作方法

操作之前必须知道如何使用OGNL表达式,并且了解OGNL表达式的取值范围只能在其context和root中,格式为

Ognl.getValue(expression,context,root);
//expression:为我们编写的ognl表达式,从后两个参数中获取值,获取规则会从下面的例子中详细讲解
//context:ognl的上下文,类型为map,
//root:ognl的根,可以为javabean、list、map、.... 等等很多值
package other;

import ognl.Ognl;
import ognl.OgnlException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OgnlLearn {
    public static void main(String[] args) throws Exception{
        // Ognl.getValue(expression,context,root);
        test1();   // 取值
        test2();
        test3();
        test4();
        test5();   // 设定值
        test6();
        test7();   // 其他操作
        test8();
        test9();
        test10();
    }

    public static void test1() throws OgnlException {
        User user = new User();
        user.setName("monster");
        String name = (String) Ognl.getValue("name",new HashMap(),user);
        System.out.println(name);
    }

    public static void test2() throws OgnlException {
        User user = new User();
        user.setAge(2);
        int name = (Integer) Ognl.getValue("age",new HashMap(),user);
        System.out.println(name);
    }

    public static void test3() throws OgnlException {
        User user = new User();
        user.setPassword("123456");
        String name = (String) Ognl.getValue("password",new HashMap(),user);
        System.out.println(name);
    }

    public static void test4() throws OgnlException {
        User user = new User();
        Address address = new Address();
        address.setCity("beijing");
        address.setStreet("haidian");
        user.setAddress(address);
        String city = (String) Ognl.getValue("address.city",new HashMap(),user);
        String street = (String) Ognl.getValue("address.street",new HashMap(),user);
        System.out.println(city + "***" + street);
    }

    public static void test5() throws OgnlException {
        User user = new User();
        Ognl.getValue("name = 'monster1'",new HashMap(),user);
        System.out.println(user.getName());
    }

    public static void test6() throws OgnlException {
        User user = new User();
        Address address = new Address();
        address.setCity("shanghai");
        user.setAddress(address);
        //Ognl.getValue("address = address",new HashMap(),user);
        String city = (String) Ognl.getValue("address.city",new HashMap(),user);

        System.out.println(city);

    }

    public static void test7() throws OgnlException{
        List<User> uList = new ArrayList<>();
        User user1 = new User();
        user1.setName("monster1");
        uList.add(user1);
        User user2 = new User();
        user2.setName("monster2");
        uList.add(user2);
        System.out.println(Ognl.getValue("[0].name",new HashMap(),uList));
        System.out.println(Ognl.getValue("[1].name",new HashMap(),uList));
    }

    public static void test8() throws OgnlException{
        Map<String,String> mymap = new HashMap<>();
        mymap.put("key1","value1");
        mymap.put("key2","value2");

        System.out.println(Ognl.getValue("key1",new HashMap(),mymap));
        System.out.println(Ognl.getValue("key2",new HashMap(),mymap));
    }

    public static void test9() throws OgnlException{
        User user = new User();
        List<String> nameList = (List<String>) Ognl.getValue("{'TOM','PETER'}",new HashMap(),user);
    }

    public static void test10() throws OgnlException{
        User user = new User();
        Map<String,String> mymap = (Map<String, String>) Ognl.getValue("#{'TOM':'cat','PETER':'bigdog'}",new HashMap(),user);
    }

}

user.java

package other;

public class User {
    private String name;
    private String password;
    private int age;
    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

address.java

package other;

public class Address {
    private String city;
    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

Ognl 与 Struts2 的集合

OGNL中的上下文即struts2中的actionContext
OGNL中的root即struts2中的valueStack

actionContext 与 valueStack 的关系

ActionContext:
充当OGNL的context。是action的上下文,也可以叫做action的数据中心,本质是一个map,在其中,所有的数据都存放在这里,那其中到底存放了哪些东西呢,actionContext中存放数据的方式又是怎样的?
actionContext是一个map,所以其中都是以键值对的形式存储对象,如下图所示,
request、session、application这种我们熟知的作用域,注意是作用域,而不是对象,
paramters:这个是表单提交的参数,全部都会放到这个map中,
attr(attributes):三个作用域所有的属性都会放在该map下,如果有重复的,那么以request域中的为准。
VALUE_STACK:值栈,存放着valueStack对象,也就是说,通过ActionContext能够获取到valueStack。


图片.png

也就是说,通过valueStack可以获取到actionContext,通过ActionContext也可以获取到valueStack。
valueStack是对root进行操作,而actionContext是对context进行操作。(root和context是OGNL中的根和上下文)

获取值栈的方式:

ActionContext.getContext.getValueStack();//常用

ActionContext.getContext.get("VALUE_STACK");

获取actionContext的方式
ActionContext.getContext();  //常用

valueStack.getContext();

上一篇下一篇

猜你喜欢

热点阅读