日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

struts OGNL表达式

發布時間:2023/12/9 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 struts OGNL表达式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

OGNLContext對象有兩部分構成

  一部分是ROOT:可以放置任何對象作為ROOT

  另外一部分Context:必須是Map形式(鍵值對)

  

OGNL表達式操作

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表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//取root中userRoot對象的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表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//取User1對象的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表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//給Roog中userRoot對象的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表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//給Roog中userRoot對象的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//調用靜態方法,或者靜態屬性public void fun4() throws OgnlException{//OGNL表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//給Roog中userRoot對象的name屬性賦值Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());//賦值 返回值為null System.out.println(pi);}@Test//創建集合 list|mappublic void fun5() throws OgnlException{//OGNL表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//創建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());//創建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());} }

?

轉載于:https://www.cnblogs.com/ms-grf/p/7350551.html

總結

以上是生活随笔為你收集整理的struts OGNL表达式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。