OGNL表达式

介绍

OGNL:对象视图导航语言(Object Graphic Navigation Language)Struts框架使用OGNL作为默认的表达式语言

 ${user.addr.name} 这种写法就叫对象视图导航

OGNL不仅可以视图导航 支持比EL表达式更加丰富的功能

详情请看代码

代码示例:

bean类

package cn.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;
	}


	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}
	
	
}

demo 示例

package cn.ognl;

import cn.bean.User;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

public class Demo {

    @Test
    //准备工作+基本语法
    public void fun1() throws OgnlException {
        //准备OGNLContext
            //准备root
        User rootUser = new User("小明",18);
        //准备Context
        HashMap<String, User> context = new HashMap<>();
        context.put("user1",new User("小小",18));
        context.put("user2",new User("大大",22));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);

        //书写OGNL
        String name = (String) Ognl.getValue("name", oc, oc.getRoot());
        Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(age);
    }

    @Test
    //取出context属性值
    public void fun2() throws OgnlException {
        //准备OGNLContext
        //准备root
        User rootUser = new User("小小",18);
        //准备Context
        HashMap<String, User> context = new HashMap<>();
        context.put("user1",new User("大大",18));
        context.put("user2",new User("哈哈",22));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);

        //书写OGNL
        String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
        String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
        Integer age = (Integer) Ognl.getValue("#user1.age", oc, oc.getRoot());
        Integer age2 = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(name2);
        System.out.println(age);
        System.out.println(age2);
    }

    @Test
    //为属性赋值
    public void fun3() throws OgnlException {
        //准备OGNLContext
        //准备root
        User rootUser = new User("嘻嘻",18);
        //准备Context
        HashMap<String, User> context = new HashMap<>();
        context.put("user1",new User("哈哈",18));
        context.put("user2",new User("hiahia",22));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);

        //书写OGNL
        Ognl.getValue("name='Jerry'", oc, oc.getRoot());
        String name = (String) Ognl.getValue("name", oc, oc.getRoot());
        System.out.println(name);

        String name2 = (String) Ognl.getValue("#user1.name='马云',#user1.name", oc, oc.getRoot());
        System.out.println(name2);
    }

    @Test
    //调用方法
    public void fun4() throws OgnlException {
        //准备OGNLContext
        //准备root
        User rootUser = new User("二蛋",18);
        //准备Context
        HashMap<String, User> context = new HashMap<>();
        context.put("user1",new User("大蛋",18));
        context.put("user2",new User("小蛋",22));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);

        //书写OGNL
        Ognl.getValue("setName('lilei')", oc, oc.getRoot());
        String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());

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


        System.out.println(name);
        System.out.println(name2);
    }

    @Test
    //调用静态方法
    public void fun5() throws OgnlException {
        //准备OGNLContext
        //准备root
        User rootUser = new User("马云",18);
        //准备Context
        HashMap<String, User> context = new HashMap<>();
        context.put("user1",new User("java",18));
        context.put("user2",new User("PHP",22));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);

        //书写OGNL
        String name = (String) Ognl.getValue("@cn.ognl.HahaUtils@echo('hello 二蛋!')", oc, oc.getRoot());
        //Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
        Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(pi);
    }

    @Test
    //基本语法演示
    //ognl创建对象-list|map
    public void fun6() throws OgnlException {
        //准备OGNLContext
        //准备root
        User rootUser = new User("tom", 18);
        //准备Context
        HashMap<String, User> context = new HashMap<>();
        context.put("user1", new User("jack", 18));
        context.put("user2", new User("rose", 22));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);

        //书写OGNL
        //创建list对象
        Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
        String name = (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());

		/*System.out.println(size);
		System.out.println(name);
		System.out.println(name2);*/
        //创建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());
        System.out.println(size2);
        System.out.println(name3);
        System.out.println(age);
    }
}

静态 类

public class HahaUtils {
	
	//回应方法
		public static Object echo(Object o){
			return o;
		}
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值