struts OGNL表达式
生活随笔
收集整理的這篇文章主要介紹了
struts OGNL表达式
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
OGNLContext對(duì)象有兩部分構(gòu)成
一部分是ROOT:可以放置任何對(duì)象作為ROOT
另外一部分Context:必須是Map形式(鍵值對(duì))
OGNL表達(dá)式操作
package cn.future.a_ognl;import java.util.HashMap; import java.util.Map;import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException;import org.junit.Test;import cn.future.domain.User;public class Demo {@Test//取出Root中的值public void fun() throws OgnlException{//OGNL表達(dá)式//準(zhǔn)備ROOTUser userRoot = new User("ms",25);//準(zhǔn)備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書(shū)寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//取root中userRoot對(duì)象的name屬性String name = (String) Ognl.getValue("name", oc, oc.getRoot());int age = (Integer) Ognl.getValue("age", oc, oc.getRoot());System.out.println(name);System.out.println(age);}@Test//取出Context中的值public void fun1() throws OgnlException{//OGNL表達(dá)式//準(zhǔn)備ROOTUser userRoot = new User("ms",25);//準(zhǔn)備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書(shū)寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//取User1對(duì)象的name屬性String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());int age = (Integer) Ognl.getValue("#user1.age", oc, oc.getRoot());System.out.println(name);System.out.println(age);}@Test//為屬性賦值public void fun2() throws OgnlException{//OGNL表達(dá)式//準(zhǔn)備ROOTUser userRoot = new User("ms",25);//準(zhǔn)備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書(shū)寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//給Roog中userRoot對(duì)象的name屬性賦值Ognl.getValue("name='grf'", oc, oc.getRoot());//賦值 有返回值,返回值是name的值String name = (String) Ognl.getValue("name='grf',name", oc, oc.getRoot());//即賦值又取值//給Context中user1的name屬性賦值Ognl.getValue("#user1.name='grf'", oc, oc.getRoot());}@Test//為屬性賦值(set get)public void fun3() throws OgnlException{//OGNL表達(dá)式//準(zhǔn)備ROOTUser userRoot = new User("ms",25);//準(zhǔn)備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書(shū)寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//給Roog中userRoot對(duì)象的name屬性賦值Ognl.getValue("setName('grf')", oc, oc.getRoot());//賦值 返回值為nullString name = (String) Ognl.getValue("getName()", oc, oc.getRoot());//即賦值又取值//給Context中user1的name屬性賦值Ognl.getValue("#user1.setName('grf'),#user1.getName()", oc, oc.getRoot());}@Test//調(diào)用靜態(tài)方法,或者靜態(tài)屬性public void fun4() throws OgnlException{//OGNL表達(dá)式//準(zhǔn)備ROOTUser userRoot = new User("ms",25);//準(zhǔn)備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書(shū)寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//給Roog中userRoot對(duì)象的name屬性賦值Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());//賦值 返回值為null System.out.println(pi);}@Test//創(chuàng)建集合 list|mappublic void fun5() throws OgnlException{//OGNL表達(dá)式//準(zhǔn)備ROOTUser userRoot = new User("ms",25);//準(zhǔn)備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書(shū)寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//創(chuàng)建listOgnl.getValue("{'aaa','bbb','ccc','ddd'}", oc, oc.getRoot());Integer listSize = (Integer) Ognl.getValue("{'aaa','bbb','ccc','ddd'}.size()", oc, oc.getRoot());String listName = (String) Ognl.getValue("{'aaa','bbb','ccc','ddd'}[0]", oc, oc.getRoot());String listName1 = (String) Ognl.getValue("{'aaa','bbb','ccc','ddd'}.get(1)", oc, oc.getRoot());//創(chuàng)建mapOgnl.getValue("#{'name':'ms','age',25}", oc, oc.getRoot());Integer mapSize = (Integer) Ognl.getValue("#{'name':'ms','age',25}.size()", oc, oc.getRoot());String mapName = (String) Ognl.getValue("#{'name':'ms','age',25}[name]", oc, oc.getRoot());Integer mapAge = (Integer) Ognl.getValue("#{'name':'ms','age',25}.get('age')", oc, oc.getRoot());} }?
轉(zhuǎn)載于:https://www.cnblogs.com/ms-grf/p/7350551.html
總結(jié)
以上是生活随笔為你收集整理的struts OGNL表达式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux中fstab文件详解
- 下一篇: 文件系统ext3的文件大小限制