layout: post
title: OGNL
subtitle: OGNL
date: 2018-05-31
author: ZL
header-img: img/20180531.jpg
catalog: true
tags:
- OGNL
OGNL(對(duì)象視圖導(dǎo)航語(yǔ)言)
${user.addr.name} 這種寫法就叫對(duì)象視圖導(dǎo)航.
OGNL不僅僅可以視圖導(dǎo)航.支持比EL表達(dá)式更加豐富的功能.
使用
導(dǎo)包
在struts2的包中就已經(jīng)包含了OGNL的包ognl-3.0.6.jar
準(zhǔn)備bean類User
package zl.bean;
public class User {
private String name;
private Integer 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;
}
public User() {
super();
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
}
取出root和context的屬性值
@Test
public void fun1() throws OgnlException{
//準(zhǔn)備root
User u1 = new User("qqq",18);
//準(zhǔn)備context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("sss", 19));
context.put("user2", new User("yyy", 20));
//設(shè)置root和context
OgnlContext ognl = new OgnlContext();
ognl.setRoot(u1);
ognl.setValues(context);
//取出root中的數(shù)據(jù),直接寫屬性名
String name = (String) Ognl.getValue("name", ognl, ognl.getRoot());
Integer age = (Integer) Ognl.getValue("age", ognl, ognl.getRoot());
System.out.println(name);
System.out.println(age);
//取出context中的屬性值
//#代表從context中取值
String user1name = (String) Ognl.getValue("#user1.name", ognl, ognl.getRoot());
Integer user1age = (Integer) Ognl.getValue("#user1.age", ognl, ognl.getRoot());
System.out.println(user1name+user1age);
}
修改root和context的屬性值
@Test
public void fun2() throws OgnlException{
//準(zhǔn)備root
User u1 = new User("qqq",18);
//準(zhǔn)備context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("sss", 19));
context.put("user2", new User("yyy", 20));
//設(shè)置root和context
OgnlContext ognl = new OgnlContext();
ognl.setRoot(u1);
ognl.setValues(context);
//修改root的name屬性值
Ognl.getValue("name = 'aaaa'", ognl, ognl.getRoot());
String rootname = (String) Ognl.getValue("name", ognl, ognl.getRoot());
System.out.println(rootname);
//修改user1的name和age屬性值
Ognl.getValue("#user1.name = 'bbbb'", ognl, ognl.getRoot());
Ognl.getValue("#user1.age = 99", ognl, ognl.getRoot());
String user1name = (String) Ognl.getValue("#user1.name", ognl, ognl.getRoot());
Integer user1age = (Integer) Ognl.getValue("#user1.age", ognl, ognl.getRoot());
System.out.println(user1name+user1age);
}
調(diào)用user的方法
@Test
public void fun3() throws OgnlException{
//準(zhǔn)備root
User u1 = new User("qqq",18);
//準(zhǔn)備context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("sss", 19));
context.put("user2", new User("yyy", 20));
//設(shè)置root和context
OgnlContext ognl = new OgnlContext();
ognl.setRoot(u1);
ognl.setValues(context);
//從root中調(diào)用user對(duì)象的getName方法浪感。
String rootname = (String) Ognl.getValue("getName()", ognl, ognl.getRoot());
System.out.println(rootname);
//從root中調(diào)用user對(duì)象的setName方法。
Ognl.getValue("setName('jjj')", ognl, ognl.getRoot());
String rootname2 = (String) Ognl.getValue("getName()", ognl, ognl.getRoot());
System.out.println(rootname2);
}
調(diào)用靜態(tài)的方法和屬性
@Test
public void fun4() throws OgnlException{
//準(zhǔn)備root
User u1 = new User("qqq",18);
//準(zhǔn)備context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("sss", 19));
context.put("user2", new User("yyy", 20));
//設(shè)置root和context
OgnlContext ognl = new OgnlContext();
ognl.setRoot(u1);
ognl.setValues(context);
//訪問靜態(tài)方法,屬性
String xxx = (String) Ognl.getValue("@zl.ognl.XXX@xxx('1234567')", ognl, ognl.getRoot());
Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", ognl, ognl.getRoot());
System.out.println(xxx);
System.out.println(pi);
}
創(chuàng)建list對(duì)象,操作list
@Test
public void fun5() throws OgnlException{
//準(zhǔn)備root
User u1 = new User("qqq",18);
//準(zhǔn)備context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("sss", 19));
context.put("user2", new User("yyy", 20));
//設(shè)置root和context
OgnlContext ognl = new OgnlContext();
ognl.setRoot(u1);
ognl.setValues(context);
//創(chuàng)建list揭斧,取出list的某個(gè)元素
Integer size = (Integer)Ognl.getValue("{'aaa','bbb','ccc','ddd'}.size()", ognl, ognl.getRoot());
String first = (String)Ognl.getValue("{'aaa','bbb','ccc','ddd'}[0]", ognl, ognl.getRoot());
String second = (String)Ognl.getValue("{'aaa','bbb','ccc','ddd'}.get(1)", ognl, ognl.getRoot());
System.out.println(size);
System.out.println(first);
System.out.println(second);
List list = (List) Ognl.getValue("{'aaa','bbb','ccc','ddd'}", ognl, ognl.getRoot());
System.out.println(list);
}
創(chuàng)建map對(duì)象讹开,操作map
@Test
public void fun6() throws OgnlException{
//準(zhǔn)備root
User u1 = new User("qqq",18);
//準(zhǔn)備context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("sss", 19));
context.put("user2", new User("yyy", 20));
//設(shè)置root和context
OgnlContext ognl = new OgnlContext();
ognl.setRoot(u1);
ognl.setValues(context);
//創(chuàng)建map,取出某個(gè)元素
Integer size = (Integer)Ognl.getValue("#{'name':'bbb','age':18}.size()", ognl, ognl.getRoot());
String name = (String)Ognl.getValue("#{'name':'bbb','age':18}['name']", ognl, ognl.getRoot());
String name2 = (String)Ognl.getValue("#{'name':'bbb','age':18}.get('name')", ognl, ognl.getRoot());
System.out.println(size);
System.out.println(name);
System.out.println(name2);
Map map = (Map)Ognl.getValue("#{'name':'bbb','age':18}", ognl, ognl.getRoot());
System.out.println(map);
}