OGNL(Object-Graph Navigation Language):图形化对象导航语言

创建实体类

Department.java

package cap.action;

public class Department {
    private int id;
    private String name;

    public Department() {
    }
    public Department(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Role .java

package cap.action;

public class Role {
    private int id;
    private String name;

    public Role() {
    }
    public Role(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

User.java

package cap.action;

public class User {
    private int id;
    private String name;
    private String password;
    private Department department;

    public User() {
    }
    public User(int id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Department getDepartment() {
        return department;
    }
    public void setDepartment(Department department) {
        this.department = department;
    }

    public int add(int a,int b){
        return a+b;
    }
    public String hello(String word){
        return "hello "+word;
    }
}

创建测试类

package cap.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ognl.Ognl;
import ognl.OgnlException;

import org.junit.Test;

import cap.action.Department;
import cap.action.Role;
import cap.action.User;

public class TestOGNL {
    @Test
    public void test01() throws OgnlException {
        User user=new User(1,"青林","12345");
        Department dep=new Department(1,"中央办公室");
        user.setDepartment(dep);
        System.out.println(Ognl.getValue("id", user));  //第二个参数为指定的根,所有在当前根下的需要读取的内容都是相对根而言的
        System.out.println(Ognl.getValue("department.name", user));

         /*
          * OGNL(Object-Graph Navigation Language):图形化对象导航语言
          * 以上情况的导航图如下:
          * user
          *     --id
          *     --name
          *     --password
          *     --department
          *         --id
          *         --name
          */
    }


    @Test
    public void test02() throws OgnlException {
        Map maps=new HashMap();
        User user=new User(1,"青林","12345");
        Department dep=new Department(1,"中央办公室");
        user.setDepartment(dep);
        Role role=new Role(1,"办公室主任");
        maps.put("u", user);
        maps.put("r", role);

        //以下是在root中找
        System.out.println(Ognl.getValue("name", maps, user));  //getValue(String expression, Map context, Object root)
        System.out.println(Ognl.getValue("#root.password", maps, user));

        //以下是在map中找
        System.out.println(Ognl.getValue("#u.name", maps, user));

        //root和map一样
        System.out.println(Ognl.getValue("r.name", maps, maps));        //在root中找
        System.out.println(Ognl.getValue("#r.name", maps, maps));       //在map中找

         /*
          * 根为user时导航图如下:
          * user
          *     --id
          *     --name
          *     --password
          *     --department
          *         --id
          *         --name
          *
          * maps
          *     --u
          *         --id
          *         --name
          *         --password
          *         --department
          *             --id
          *             --name
          *     --r
          *         --id
          *         --name
          */
    }

    @Test
    public void test03() throws OgnlException {
        List users=new ArrayList();
        users.add(new User(1,"唐僧","tangseng"));
        users.add(new User(2,"悟空","wukong"));
        users.add(new User(3,"八戒","bajie"));
        users.add(new User(4,"沙僧","shaseng"));
        System.out.println(Ognl.getValue("#root[0].name", users));
        System.out.println(Ognl.getValue("get(0).name", users));    //先调用List的get方法,再访问里面的属性

        System.out.println(Ognl.getValue("#root[0].add(2,3)", users));
        System.out.println(Ognl.getValue("get(0).add(8,9)", users));

        User user=new User();
        System.out.println(Ognl.getValue("hello('struts2')", user));
    }
}

运行结果

test01

这里写图片描述

test02

OGNL(Object-Graph Navigation Language):图形化对象导航语言_第1张图片

test03

OGNL(Object-Graph Navigation Language):图形化对象导航语言_第2张图片

你可能感兴趣的:(Struts2)