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<String,Object> maps=new HashMap<String,Object>();
        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<User> users=new ArrayList<User>();
        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

这里写图片描述

test03

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值