在struts2 中使用OGNL時(shí)經(jīng)常會(huì)遇到“#”,"%","$",具體用法繞到頭暈 ,在網(wǎng)上找到下面的介紹 ,故整理摘記,防止一暈到底。
“#”主要有3種用途,
1.訪問(wèn)OGNL上下文和Action上下文 ,相當(dāng)于ActionContext.getContext();下面是幾個(gè)ActionContext 中有用的屬性
parameter : #parameter.id[0] ~~request.getParameter("id");
request : #request.id ~~request.getAttribute("id");
session : #session.id ~~session.getAttribute("id");
application: #application.id ~~application.getAttribute("id");
attr : #attr.id ~~依次按照request ->session ->application 順序查找id屬性的值。
2用于過(guò)濾和投影集合,如 books.{?#this.price <100};
3.夠著Map ,如 #{‘foo’:‘a(chǎn)k’,'boo':'kk'};
"%"用途是在標(biāo)志屬性為字符串的時(shí)候用于計(jì)算OGNL表達(dá)式的值。
如 <s:url value = "%{#foobar['foo']}"/>
$主要有兩種用途
1.在國(guó)際化資源文件中引用OGNL表達(dá)式。
2.在struts2配置文件中使用OGNL表達(dá)式。
找到的一段示例代碼
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title></title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- </head>
-
- <body>
- ognl.jsp.....................<br>
- 編號(hào): <s:property value="id"/><br>
- 地址:<s:property value="address"/><br>
- User對(duì)象:<s:property value="user"/><br>
- userName:<s:property value="user.userName"/><br>
- userPwd:<s:property value="user.userPwd"/><br>
- userList集合:<s:property value="userList"/><br>
- list size:<s:property value="userList.size"/><br>
- 第一個(gè)對(duì)象:<s:property value="userList[0]"/><br>
- 投影<br>
- 取得List中所有的姓名:<s:property value="userList.{userName}"/><br>
- 取得List中第一個(gè)對(duì)象的姓名:<s:property value="userList.{userName}[0]"/>
- 輸出List中姓名為tom的所有用戶對(duì)象:<br>
- <s:property value="userList.{?#this.userName=='tom'}"/><br>
- 輸出List中姓名為tom的第一個(gè)用戶對(duì)象:<br>
- <s:property value="userList.{^#this.userName=='tom'}"/><br>
- 輸出List中姓名為tom的最后一個(gè)用戶對(duì)象:<br>
- <s:property value="userList.{$#this.userName=='tom'}"/><br>
-
- OGNL操作Map集合--------------------<br>
- 輸出Map中的key:<s:property value="userMap.keys"/><br>
- 輸出Map中的value:<s:property value="userMap.values"/><br>
- 輸出Map中的數(shù)量:<s:property value="userMap.size"/><br>
- 輸出key(tom)對(duì)應(yīng)的value:<s:property value="userMap['tom'].{id}[0]"/><br>---
- 判斷Map是否為empty:<s:property value="userMap.isEmpty"/><br>
-
- 動(dòng)態(tài)創(chuàng)建List和Map ---------------------------<br>
- 動(dòng)態(tài)創(chuàng)建List:<s:property value="{'aa','bb','cc'}"/>
- 動(dòng)態(tài)創(chuàng)建Map:<s:property value="#{'aa':'aa','bb':'bb'}"/>
- <br>
- 獲取request session Application中的值--------------------------<br>
- 獲取Session中的值:<br>
- <s:property value="#session.sessionKey"/><br>
- 獲取reuqst中的值:<br>
- <s:property value="#request.requestKey"/><br>
- 獲取Application中的值:<br>
- <s:property value="#application.applicationKey"/>
- </body>
- </html>
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。