使用之前,需要使用到包分别是: javassist-3.11.0.GA.jar 和 ognl-3.0.6.jar
如图:
Cat 实体类
package entity;
/**
* Cat 实体类
* */
public class Cat {
private int id;//编号
private int age;//年龄
private String name;//姓名
private String color;//颜色
public void catSay(){
System.out.println("这是一只猫在叫的方法");
}
/*
* 构造方法
* */
public Cat() {
}
public Cat(int id, int age, String name, String color) {
this.id = id;
this.age = age;
this.name = name;
this.color = color;
}
/*
* 封装方法
* */
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Dog 实体类
package entity;
import java.util.ArrayList;
import java.util.List;
/**
* Dog 实体类
* */
public class Dog {
private int id;//编号
private int age;//年龄
private String name;//姓名
private String color;//颜色
private List<Cat> cats=new ArrayList<Cat>();
public void dogSay(){
System.out.println("这是一只狗在叫的方法");
}
/*
* 构造方法
* */
public Dog() {
}
public Dog(int id, int age, String name, String color) {
this.id = id;
this.age = age;
this.name = name;
this.color = color;
}
/*
* 封装方法
* */
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public List<Cat> getCats() {
return cats;
}
public void setCats(List<Cat> cats) {
this.cats = cats;
}
}
使用OGNL表达式
package test;
import java.util.ArrayList;
import java.util.List;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import entity.Cat;
import entity.Dog;
/*
* 使用OGNL表达式
* */
public class Test {
private static OgnlContext ognlContext = new OgnlContext();//OGNL上下文
private static Object root= new Object();//根对象,只做点位符使用
public static void main(String[] args) throws OgnlException {
/*
* 创建对象传参,为下面的操作准备
* */
Cat cat1 = new Cat(1, 3, "一猫", "绿色");
Cat cat2 = new Cat(2, 4, "二猫", "蓝色");
Dog dog1 = new Dog(1, 5, " 一狗", "红色");
Dog dog2 = new Dog(2, 6, "二狗", "青色");
dog1.getCats().add(cat1);
dog1.getCats().add(cat2);
/*
* 所有对象都放入 OgnlContext (其实就是一个Map)上,接下来的代码会用到
* */
ognlContext.put("cat1", cat1);
ognlContext.put("cat2", cat2);
ognlContext.put("dog1", dog1);
ognlContext.put("dog2", dog2);
List<Object> annimals=new ArrayList<Object>();
annimals.add(dog1);
annimals.add(dog2);
annimals.add(cat1);
ognlContext.put("annimals", annimals);
ognlContext.setRoot(cat1);
//输出cat1的信息
printDog1();
//输出通过 List 形式找到具体的信息
printList();
//输出 Integer 类的二进制
printToBinaryString();
//输出一个数组的其中一个
printArray();
//输出根据Key找到值
printKey();
//输出通过 List 形式找到具体的信息
printList2();
//使用三元运算符
printOther();
}
private static void printDog1() throws OgnlException {
root=Ognl.getValue("#dog1.id", ognlContext,ognlContext.getRoot());
System.out.println("dog1.id="+root);
root=Ognl.getValue("#dog1.age", ognlContext,ognlContext.getRoot());
System.out.println("dog1.age="+root);
root=Ognl.getValue("#dog1.name", ognlContext,ognlContext.getRoot());
System.out.println("dog1.name="+root);
root=Ognl.getValue("#dog1.color", ognlContext,ognlContext.getRoot());
System.out.println("dog1.color="+root);
System.out.println("-----------------------");
}
private static void printList() throws OgnlException {
root=Ognl.getValue("#dog1.cats[0].color", ognlContext,ognlContext.getRoot());
System.out.println("cats[0].color="+root);
root=Ognl.getValue("#dog1.cats[1].name", ognlContext,ognlContext.getRoot());
System.out.println("cats[1].name="+root);
System.out.println("-----------------------");
}
private static void printToBinaryString() throws OgnlException{
System.out.println(Integer.toBinaryString(10));
root =Ognl.getValue("@java.lang.Integer@toBinaryString(10)", ognlContext,ognlContext.getRoot());
System.out.println(root);
System.out.println("-----------------------");
}
private static void printArray() throws OgnlException{
root=Ognl.getValue("{'aaa','bbb','ccc'}[2]", ognlContext, ognlContext.getRoot());
System.out.println(root);
System.out.println("-----------------------");
}
private static void printKey() throws OgnlException{
root=Ognl.getValue("#{ 'aaa' : 'aaa value', 'bbb' : 'bbb value' }['aaa']", ognlContext, ognlContext.getRoot());
System.out.println(root);
System.out.println("-----------------------");
}
private static void printList2() throws OgnlException {
root=Ognl.getValue("#dog1.cats.{age}", ognlContext, ognlContext.getRoot());
System.out.println(root);
System.out.println("-----------------------");
}
private static void printOther() throws OgnlException {
System.out.println(Ognl.getValue("#dog1.cats.{#this.name.length()<3 ?'Hello World':#this.name}", ognlContext, ognlContext.getRoot()));
}
}
效果图: