OGNL使用方法总结
生活随笔
收集整理的這篇文章主要介紹了
OGNL使用方法总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、訪問屬性
名字屬性獲取:<s:property value="user.username"/><br>地址屬性獲取:<s:property value="user.address.addr"/><br>2、訪問方法
調用值棧中對象的普通方法:<s:property value="user.get()"/><br>3、訪問靜態屬性和方法
調用Action中的靜態方法: <s:property value="@struts.action.LoginAction@get()"/>調用JDK中的類的靜態方法: <s:property value="@java.lang.Math@floor(44.56)"/><br>調用JDK中的類的靜態方法(同上): <s:property value="@@floor(44.56)"/><br>調用JDK中的類的靜態方法: <s:property value="@java.util.Calendar@getInstance()"/><br>調用普通類中的靜態屬性: <s:property value="@struts.vo.Address@TIPS"/><br>4、訪問構造方法
調用普通類的構造方法: <s:property value="new struts.vo.Student('李曉紅' , '美女' , 3, 25).username"/>5、訪問數組
獲取List:<s:property value="testList"/><br>獲取List中的某一個元素(可以使用類似于數組中的下標獲取List中的內容):<s:property value="testList[0]"/><br>獲取Set:<s:property value="testSet"/><br>獲取Set中的某一個元素(Set由于沒有順序,所以不能使用下標獲取數據):<s:property value="testSet[0]"/><br> ×獲取Map:<s:property value="testMap"/><br>獲取Map中所有的鍵:<s:property value="testMap.keys"/><br>獲取Map中所有的值:<s:property value="testMap.values"/><br>獲取Map中的某一個元素(可以使用類似于數組中的下標獲取List中的內容):<s:property value="testMap['m1']"/><br>獲取List的大小:<s:property value="testSet.size"/><br>6、訪問集合 – 投影、選擇(? ^ $)
利用選擇獲取List中成績及格的對象: <s:property value="stus.{?#this.grade>=60}"/><br>利用選擇獲取List中成績及格的對象的username:<s:property value="stus.{?#this.grade>=60}.{username}"/><br>利用選擇獲取List中成績及格的第一個對象的username:<s:property value="stus.{?#this.grade>=60}.{username}[0]"/><br>利用選擇獲取List中成績及格的第一個對象的username:<s:property value="stus.{^#this.grade>=60}.{username}"/><br>利用選擇獲取List中成績及格的最后一個對象的username:<s:property value="stus.{$#this.grade>=60}.{username}"/><br>利用選擇獲取List中成績及格的第一個對象然后求大小:<s:property value="stus.{^#this.grade>=600}.{username}.size"/><br>7、集合的偽屬性
OGNL能夠引用集合的一些特殊的屬性,這些屬性并不是JavaBeans模式,例如size().length()等等. 當表達式引用這些屬性時,OGNL會調用相應的方法,這就是偽屬性.
8、Lambda :[…]
格式::[…]
要了解更多內容請查看OGNL表達式官方文檔:
http://commons.apache.org/proper/commons-ognl/language-guide.html
總結
以上是生活随笔為你收集整理的OGNL使用方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OGNL基础
- 下一篇: 【struts2】struts2拦截器