Struts——OGNL表達式與Struts2結合
阿新 • • 發佈:2018-02-27
highlight get ble ue4 cep 語法 ddr img println
一、OGNL表達式
OGNL:對象視圖導航語言. ${user.addr.name} 這種寫法就叫對象視圖導航.
OGNL不僅僅可以視圖導航而且還支持比EL表達式更加豐富的功能.
語法:
public void func() throws OgnlException { // 1.準備Root User rootUser = new User("jiaxin",23); // 2.準備Context Map<String,User> context = new HashMap<String,User>(); context.put("user1",new User("計震宇",22)); context.put("user2",new User("baoyan",25)); // 3.實例化OnglContext OgnlContext oc = new OgnlContext(); // 4.設置進OnglContext中 oc.setRoot(rootUser); oc.setValues(context); // ***************從root中取值*************** String name = (String) Ognl.getValue("name", oc, oc.getRoot()); Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot()); System.out.println("name:"+name+"\tage:"+age); // ***************從context中取值*************** String name1 = (String) Ognl.getValue("#user1.name", oc, oc.getRoot()); Integer age1 = (Integer) Ognl.getValue("#user1.age", oc, oc.getRoot()); System.out.println("name:"+name1+"\tage:"+age1); // ***************賦值**************** Ognl.getValue("name=‘計寶寶‘", oc, oc.getRoot()); String name2 = (String) Ognl.getValue("name", oc, oc.getRoot()); // 先改值再取值 String name3 = (String) Ognl.getValue("#user1.name=‘計寶寶‘,#user1.name", oc, oc.getRoot()); System.out.println(name2+"\t"+name3); // *****************調普通方法**************** String name4 = (String) Ognl.getValue("setName(‘計震宇‘),getName()", oc, oc.getRoot()); String name5 = (String) Ognl.getValue("#user2.setName=‘張軍‘,#user2.getName()", oc, oc.getRoot()); System.out.println(name4+"\t"+name5); // *****************調用靜態方法***************** Double PI = (Double) Ognl.getValue("@java.lang.Math@PI",oc,oc.getRoot()); Double PI2 = (Double) Ognl.getValue("@@PI",oc,oc.getRoot()); System.out.println(PI); // ******************創建List,Map對象**************** Integer len = (Integer) Ognl.getValue("{‘ji‘,‘zhen‘,‘yu‘,‘bao‘}.size()", oc, oc.getRoot()); String value = (String) Ognl.getValue("{‘ji‘,‘zhen‘,‘yu‘,‘bao‘}.get(0)", oc, oc.getRoot()); String value2 = (String) Ognl.getValue("{‘ji‘,‘zhen‘,‘yu‘,‘bao‘}[2]", oc, oc.getRoot()); System.out.println(len+"\t"+value+"\t"+value2); // 字典前面要加“#”,註意和取context中的內個“#”沒有任何關系 Integer len2 = (Integer) Ognl.getValue("#{‘k1‘:‘v1‘,‘k2‘:‘v2‘}.size()",oc,oc.getRoot()); String value3 = (String) Ognl.getValue("#{‘k1‘:‘v1‘,‘k2‘:‘v2‘}[‘k1‘]",oc,oc.getRoot()); String value4 = (String) Ognl.getValue("#{‘k1‘:‘v1‘,‘k2‘:‘v2‘}[‘k2‘]",oc,oc.getRoot()); System.out.println(len2+"\t"+value3+"\t"+value4); }
二、OGNL表達式與Struts2結合
1、三種接收參數的原理
2、獲取參數
3、查找順序
Struts——OGNL表達式與Struts2結合