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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/280798.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280798.shtml
英文地址,請注明出處:http://en.pswp.cn/news/280798.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

纖程(FIBER)

Indy 10 還包含對纖程的支持。纖程是什么&#xff1f;簡單來說&#xff0c;它也是 一個“線程”&#xff0c;但是它是由代碼控制的&#xff0c;而不是由操作系統控制的。實際上&#xff0c;可以認為線程 是一個高級纖程。纖程和 Unix 用戶線程(Unix user threads)很相似。 線程…

制作一個用戶頭像選擇器仿 WeGame

制作一個用戶頭像選擇器仿 WeGameCropAvatar作者&#xff1a;WPFDevelopersOrg - 驚鏵原文鏈接&#xff1a;https://github.com/WPFDevelopersOrg/WPFDevelopers框架使用.NET40&#xff1b;Visual Studio 2019;制作一個用戶頭像選擇Canvas為父控件所實現&#xff0c;展示圖片使…

PS2019進階筆記(二)

云端網校筆記&#xff08;PS2015&#xff09; 一、圖層混合模式 圖層面板上的左上角&#xff0c;默認正常 混合下面圖層&#xff0c;下面正片&#xff08;如模特&#xff09;。 最常用是&#xff1a;不透明度 溶解&#xff1a;需調節透明度 變暗組&#xff1a; 亮區域去除…

Html5里frameSet不在使用的替代方法,使用ifram

原來得使用方式&#xff1a; <frameset rows"100,*" frameborder"0"><frame name"header" src"header.aspx"></frame><frameset cols"15%, *"><frame name"menu" src"left.aspx&…

網曝南方電網搞末位淘汰和裁員,給各下屬單位強制規定辭退率和降崗降級率!...

電網作為壟斷性國企&#xff0c;在人們心中一向是好單位的代名詞&#xff0c;但最近卻有網友曝光南方電網搞末位淘汰和裁員&#xff0c;給各單位下了辭退率和降崗降級率&#xff0c;每個單位都要開除一部分人&#xff0c;或者把一部分人崗級降下來。有南方電網員工馬上跑出來辟…

二維數組中的查找

2019獨角獸企業重金招聘Python工程師標準>>> 題目 在一個二維數組中&#xff0c;每一行中的數都按照從左到右、從上到下的遞增順序排列。要求輸入一個整數&#xff0c;判斷數組中是否存在該整數 實現代碼 function find($matrix, $rows, $columns, $key) {//TODO 參…

C# JObject轉換JSON文件相關處理

一、JObject.Parse 1.把整個json文件字符串轉化成JObject格式。 JObject jsonData JObject.Parse(jsonString); 2.逐級轉換成JObject 最低級是"Device": "Windowsr"&#xff0c;字典。 jsonData JObject.Parse(jsonData[jsonName][jsonIndex].ToStr…

通過修改然后commit的方式創建自己的鏡像

創建自己的鏡像&#xff1a;通過現有的鏡像來創建自己的鏡像。1、首先拉取一個鏡像到本地$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu 12.04 5b117edd0b76 11 months…

hdu6103[尺取法] 2017多校6

/*hdu6103[尺取法] 2017多校6*/ #include <bits/stdc.h> using namespace std; int T, m; char str[20005]; void solve() {int ans 0;int n strlen(str);for (int i 0; i < n; i) {int l 0, r 0, p1 i, p2 i 1, cost 0;while (p1 - r > 0 && p2 …

藍牙追蹤_如何使用藍牙追蹤器跟蹤您的東西

藍牙追蹤We’ve all done it: you misplace something important and you spend a lot of time (with a lot of stress) backtracking to locate it again. With Bluetooth tracking devices you can make the hunt a lot easier, less stressful, and even avoid losing the t…

遠程訪問CENTOS的MYSQL數據庫設置

遠程訪問CENTOS的MYSQL數據庫設置 mysql -u root grant all privileges on *.* to root%identified by root; 后面的root是root用戶的密碼 grant all privileges on *.* to rootlocalhostidentified by root; mysql -u root -p

ShardingCore 7.0 版本發布

NCC ShardingCore 是一款 EntityFramework Core based 高性能、輕量級、分表分庫、讀寫分離解決方案&#xff0c;具有零依賴、零學習成本、零業務代碼入侵等特點。ShardingCore 7.0 已于近期發布。從 ShardingCore 7.0 開始&#xff0c;啟用版本號第二位來對應不同的 EFCore 版…

搬運 centos7.2 apache 綁定二級目錄 訪問依然是apache頁面

<VirtualHost *:80>ServerName xx.comDocumentRoot /var/www/html/xx</VirtualHost> 轉載于:https://www.cnblogs.com/kiimi/p/8662490.html

django09: ORM以及CSRF(舊筆記)

ORM相當于程序里的數據庫操作 ORM(一) https://www.cnblogs.com/liwenzhou/p/8688919.html ORM(二) https://www.cnblogs.com/liwenzhou/p/8660826.html CSRF:防止網站請求偽造&#xff0c;即釣魚網 在Form表達添加&#xff1a;{% csrf_toker %}

vba發送郵件 簽名_如何更改“從Windows 10的郵件發送”簽名

vba發送郵件 簽名The Windows 10 Mail app is a decent email client that allows you to add other email accounts in addition to your Microsoft accounts. You’ll notice, though, that any emails you write in the Mail app have a default signature. Windows 10 Mail…

JAVA_SE基礎——24.面向對象的內存分析

黑馬程序猿入學blog ... 接著上一章的代碼&#xff1a; //車類 class Car{//事物的公共屬性使用成員變量描寫敘述。String name; //名字的屬性 String color; //顏色屬性 int wheel; //輪子數 //事物的公共行為使用函數描寫敘述。 public void run(){ System.out.println(name&…

煮茶社區AVR開發板第二版[轉]

原圖:http://blossom.cnblogs.com/gallery/image/21891.html

[Kogel.Subscribe.Mssql]SQL Server增量訂閱,數據庫變更監聽

此框架是SQL Server增量訂閱&#xff0c;用來監聽增刪改數據庫數據變更目前僅支持SQL Server&#xff0c;后續會支持MySQL和Oracle&#xff0c;Nuget上可以下載安裝或者使用Nuget命令添加包dotnet add package Kogel.Subscribe.Mssql --version 0.0.0.1可以用來處理DB主從同步&…

java instanceof運算符

instanceof 運算符 該運算符用于操作對象實例&#xff0c;檢查該對象是否是一個特定類型&#xff08;類類型或接口類型&#xff09;。 instanceof運算符使用格式如下&#xff1a; ( Object reference variable ) instanceof (class/interface type) 如果運算符左側變量所指的對…

C# XML添加刪除/SelectNodes/xpath

SelectNodes中的XPath //從當前節點的兒子節點中選擇名稱為 item 的節點。 SelectNodes("item")//從根節點的兒子節點中選擇名稱為 item 的節點。 SelectNodes("/item")// 從任意位置的節點上選擇名稱為 item 的節點。要重點突出這個任意位置&#xff0c;…