Ognl基本使用
---恢复内容开始---
Ognl默认是从“根”中取数据的
下面Demo中用的是
这个方法。
// 参数1,填写Ognl表达式
// 参数2,Map类=》context上下文
// 参数3,JavaBean,Map,List````等类型,Root 根
Ognl需要的jar包:
先准备两个JavaBean
public class User { private String name; private int age; private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class Address { private String city; public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
下面列举Demo,方法中使用Ognl
public void func1() throws OgnlException { User user = new User(); user.setName("Tom"); String name = (String) Ognl.getValue("name", new HashMap(), user); System.out.println(name); }
//从List根中取数据 public void func2() throws OgnlException { List<User> list=new ArrayList<User>(); User user = new User(); user.setName("Tom"); list.add(user); User user2=new User(); user2.setName("Jerry"); list.add(user2); //[1]代表list中第二个数据 String name = (String) Ognl.getValue("[1].name", new HashMap(), list); System.out.println(name); }
//稍复杂的从JavaBean中取数据 public void func4() throws OgnlException{ User user=new User(); Address address=new Address(); address.setCity("北京"); user.setAddress(address); String city=(String) Ognl.getValue("address.city", new HashMap(),user); System.out.println(city); }
//从第二个参数Map中取数据 public void func3() throws OgnlException{ Map<String, Object> context=new HashMap<String,Object>(); context.put("name", "Tom"); //为了验证确实是取自Map中,添加一个JavaBean User user=new User(); user.setName("Jerry"); //如果取自Map,则需要在name前加#,否则默认取自第三个JavaBean中 String name = (String) Ognl.getValue("#name",context, user); System.out.println(name); }
// 用Ognl表达式用getValue方法给对象赋值 public void func5() throws OgnlException { User user = new User(); Integer age = (Integer) Ognl.getValue("age=23", new HashMap(), user); System.out.println(user.getAge()); System.out.println(age); }
// 用Ognl表达式用setValue方法给对象赋值 public void func6() throws OgnlException { User user = new User(); Ognl.setValue("age", new HashMap(), user,23); System.out.println(user.getAge()); }
在User类中新增一个非静态方法和一个静态方法
public static String getNowTime1(){ return new SimpleDateFormat("yyyy/MM/dd").format(new Date()); } public String getNowTime2(){ return new SimpleDateFormat("yyyy-MM-dd").format(new Date()); }
// 调用根中方法 public void func7() throws OgnlException { // 静态方法和普通方法调用方式一样 User user = new User(); String nowTime1 = (String) Ognl.getValue("getNowTime1()", new HashMap<>(), user); String nowTime2 = (String) Ognl.getValue("getNowTime2()", new HashMap<>(), user); System.out.println(nowTime1); System.out.println(nowTime2); }
为了测试调用其他类中方法,新增一个类
import java.text.SimpleDateFormat; import java.util.Date; public class DateUtils { public static String getNowTime1(String format) { return new SimpleDateFormat(format).format(new Date()); } public String getNowTime2(String format) { return new SimpleDateFormat(format).format(new Date()); } }
// 调用其他类静态方法并传参数 public void func8() throws OgnlException { // 使用@符号调用其他类中静态方法,不再受必须在根中的限制 User user = new User(); String nowTime = (String) Ognl.getValue("@cn.hjp.utils.DateUtils@getNowTime1('yyyy-MM/dd')", new HashMap<>(), user); System.out.println(nowTime); }
在DateUtils类中新增一公共变量 public static Double d=12.34;进行下面测试
//调用其他类中静态变量 public void func9() throws OgnlException{ User user=new User(); Double d=(Double) Ognl.getValue("@cn.hjp.utils.DateUtils@d", new HashMap<>(), user); System.out.println(d); }
//进行运算 public void func10() throws OgnlException{ User user=new User(); Integer d=(Integer) Ognl.getValue("1+1", new HashMap<>(), user); System.out.println(d); }
// 逗号“,”连接符的使用 public void func11() throws OgnlException { User user = new User(); // 多表达式,逗号分隔,都参与运算,只是最后一个才会返回结果 String name = (String) Ognl.getValue("name='Tom',name", new HashMap<>(), user); Integer age = (Integer) Ognl.getValue("name='Tom',age=23", new HashMap<>(), user); System.out.println(name); System.out.println(age); }
// 创建List集合 public void func12() throws OgnlException { User user = new User(); List list = (List) Ognl.getValue("{'Tom','Lily','Jerry','Jack'}", new HashMap<>(), user); System.out.println(list); }
// 创建Map,要使用#符号,指定为Map类型 public void func13() throws OgnlException { User user = new User(); Map map = (Map) Ognl.getValue("#{'name':'Tom','age':23}", new HashMap<>(), user); System.out.println(map); }
方便演示创建对象,重写User类的ToString方法:
public String toString() {
return "User [name=" + name + ", age=" + age + ", address=" + address + "]";
}
// 使用Ognl创建对象 public void func14() throws OgnlException { User user1=new User(); User user2 = (User) Ognl.getValue("new cn.hjp.bean.User()",new HashMap<>(), user1); System.out.println(user2); }
//Ognl中的in和 not in 运算符 public void func15() throws OgnlException{ User user=new User(); Boolean exists=(Boolean) Ognl.getValue("'Tom' in {'Tom','Lily','Jerry','Jack'}", new HashMap<>(), user); Boolean notexists=(Boolean) Ognl.getValue("'Tom' not in {'Tom','Lily','Jerry','Jack'}", new HashMap<>(), user); System.out.println(exists); System.out.println(notexists); }
// 集合投影 public void func16() throws OgnlException { List<User> list = new ArrayList<User>(); User user1 = new User(); user1.setName("Tom"); user1.setAge(23); list.add(user1); User user2 = new User(); user2.setName("Jerry"); user2.setAge(24); list.add(user2); //只获取name属性 List l = (List) Ognl.getValue("#this.{name}", new HashMap<>(), list); System.out.println(l); }
// 集合选择 public void func17() throws OgnlException { List<User> list = new ArrayList<User>(); User user1 = new User(); user1.setName("Tom"); user1.setAge(17); list.add(user1); User user2 = new User(); user2.setName("Jerry"); user2.setAge(24); list.add(user2); //只获取name属性 List l = (List) Ognl.getValue("#this.{?age>18}", new HashMap<>(), list); System.out.println(l); }