浅谈OGNL表达式
OGNL(Object-Graph Navigation Language):對象視圖導航語言
${user.addr.name}這樣的寫法就叫對象視圖導航
OGNL不僅可以視圖導航,支持EL表達式更加豐富的功能
EL表達式取值要從11個內置對象中取:requestScope、sessionScope、applicationScope、pageScope、 pageContext、params、paramValues、header、headerValues、cookie、initParams
OGNL表達式取值要從OGNLContext中取值,在Ognl上下文中有Root部分和Context部分,Root部分放置任何對象作為root都可以,Context部分必須是Map類型
使用OGNL準備工作
導包->struts2的包中已經包含了,所以不需要導入額外的jar包
書寫代碼
@Test
//準備工作
public void fun1() throws Exception{
//準備ONGL Context
//準備ROOT
User rootUser = new User("tom","18");
//準備Context
Map<String, User> context = new HashMap<String,User>();
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 在雙引號里寫OGNL表達式即可
Ognl.getValue("", oc, oc.getRoot());
}
OGNL基本語法演示 取出root的值
@Test
//基本語法演示
//取出oot中的屬性值
public void fun2() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
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
//取出root中user對象的name屬性
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
String age = (String) Ognl.getValue("age", oc, oc.getRoot());
System.out.println(name+" "+age);
}
tom 18
OGNL取出Context的值
@Test
//基本語法演示
//取出context中的屬性值
public void fun3() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
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
String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
String age = (String) Ognl.getValue("#user2.age", oc, oc.getRoot());
System.out.println(name);
System.out.println(name2);
System.out.println(age);
}
jack
rose
22
OGNL賦值語法
@Test
//賦值
public void fun4() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
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.getValue("name='jerry'", oc, oc.getRoot());
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
//書寫OGNL
String name2 = (String) Ognl.getValue("#user1.name='zao',#user1.name", oc, oc.getRoot());
System.out.println(name);
System.out.println(name2);
}
jerry
zao
OGNL方法調用
@Test
//調用方法
public void fun5() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
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
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);
}
lilei
lucy
OGNL調用靜態方法和靜態常量
注意下面表達式
@全類名@方法名('傳參')
@全類名@常量名
@@常量名
@Test
public void fun6() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
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
String name = (String) Ognl.getValue("@cn.itheima.a_ognl.HahaUtils@echo('hello shijie')", oc, oc.getRoot());
Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
Double pi1 = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
System.out.println(name);
System.out.println(pi);
System.out.println(pi1);
}
hello shijie
3.141592653589793
3.141592653589793
OGNL創建 list map集合
@Test
public void fun7() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
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','jerr','jack'}.size()", oc, oc.getRoot());
String name = (String) Ognl.getValue("{'tom','jerr','jack'}[0]", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("{'tom','jerr','jack'}.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);
}
3
tom
jerr
2
tom
18
總結
- 上一篇: redis教程(Mac)
- 下一篇: layer弹出框的用法