版權聲明:本文爲 小異常 原創文章,非商用自由轉載-保持署名-註明出處,謝謝!
本文網址:https://blog.csdn.net/sun8112133/article/details/106677454
文章目錄
本篇博客主要來講解 Struts2 中重要的一個概念 —— OGNL。
OGNL 是一門對象圖導航語言,是一個比 EL 強大很多倍的 表達式語言,通過簡單的表達式語法,就可以存取對象的任意屬性,調用對象的方法,並且遍歷整個對象的結構圖,實現字段類型轉化等功能。
OGNL 其實是第三方的表達式語言,因爲比較火,所以 Struts2 中也引入了 OGNL 表達式語言。
一、OGNL HelloWorld
學習這門 表達式語言 我們還是以一個簡單的 HelloWorld 來講解,這裏我們分別採用 Java 環境 和 Struts2 環境 來演示。Jar 包不用變,因爲已經引入了 OGNL 的 Jar 包了。
1、Java 環境入門(瞭解)
1)訪問對象的方法
@Test
/**
* OGNL 調用對象的方法
*/
public void demo1() throws OgnlException {
// 通過 OGNL 獲得 context 對象
OgnlContext context = new OgnlContext();
// 獲得根對象
Object root = context.getRoot();
// 執行表達式,這裏的 "'helloworld'.length()" 就是 OGNL 表達式語言
Object obj = Ognl.getValue("'helloworld'.length()", context, root);
System.out.println(obj); // 10
}
2)訪問對象的靜態方法
@Test
/**
* 訪問對象的靜態方法
*/
public void demo2() throws OgnlException {
// 通過 OGNL 獲得 context 對象
OgnlContext context = new OgnlContext();
// 獲得根對象
Object root = context.getRoot();
// 執行表達式:@類名@方法名
Object obj = Ognl.getValue("@java.lang.Math@random()", context, root);
System.out.println(obj);
}
3)獲得 Root 中的數據
@Test
/**
* 訪問 Root 中的數據,不需要加 #
*/
public void demo3() throws OgnlException {
// 通過 OGNL 獲得 context 對象
OgnlContext context = new OgnlContext();
context.setRoot(new User("aaa", "123")); // username:aaa,password:123
// 獲得根對象,注意一定要先設置,再獲取
Object root = context.getRoot();
// 執行表達式
Object username = Ognl.getValue("username", context, root);
Object password = Ognl.getValue("password", context, root);
System.out.println(username + " " + password);
}
4)獲得 OgnlContext 中的數據
@Test
/**
* 訪問 Context 中的數據,需要加 #
*/
public void demo4() throws OgnlException {
// 通過 OGNL 獲得 context 對象
OgnlContext context = new OgnlContext();
// 獲得根對象
Object root = context.getRoot();
// 向 context 中存入數據
context.put("name", "張三");
// 執行表達式
Object name = Ognl.getValue("#name", context, root);
System.out.println(name); // 張三
}
2、OGNL 的 Struts2 環境入門
一定要先引入 Struts2 的標籤庫:
<%@ taglib uri="/struts-tags" prefix="s" %>
1)訪問對象的方法
<h3>調用對象的方法</h3>
<s:property value="'struts'.length()" />
2)訪問對象的靜態方法
<h3>調用對象的靜態方法</h3>
<!-- 靜態方法訪問在 Struts2 中默認是關閉的,需要開啓一個常量 -->
<s:property value="@java.lang.Math@random()" />
由於 靜態方法訪問在 Struts2 中默認是關閉的,需要開啓一個常量,所以我們需要在 Struts2 的配置文件中配置以下常量:
<struts>
<!-- 開啓 OGNL 靜態方法 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
</struts>
二、OGNL 的特殊符號
1、# 號
#
號 可以獲取 context 中的數據,也可以構建 map 集合。
1)獲取 context 的數據
<body>
<h3>獲取 context 的數據</h3>
<%
request.setAttribute("name", "張三");
%>
<s:property value="#request.name" />
</body>
2)使用 # 號構建 map 集合
<body>
<h3>構建 map 集合</h3>
<s:iterator var="i" value="{'aa','bb','cc'}">
<s:property value="i" /> -- <s:property value="#i" />
</s:iterator>
<hr>
<s:iterator var="entry" value="#{ 'aa':'11','bb':'22','cc':'33' }">
<s:property value="key" /> -- <s:property value="value" />
<s:property value="#entry.key" /> -- <s:property value="#entry.value" />
</s:iterator>
<hr>
性別:<input type="radio" name="sex1" value="男">男
<input type="radio" name="sex1" value="女">女
<br>
<s:radio list="{'男','女'}" name="sex2" label="性別" />
<br>
<s:radio list="#{'1':'男','2':'女'}" name="sex3" label="性別" />
</body>
2、% 號
%
號用於選擇是否解析 OGNL 表達式。格式:
%{}
,它能夠強制解析爲 OGNL 表達式,也可以強制不解析。注意: Struts2 的標籤不能嵌套標籤,如果嵌套會直接報錯。
1)強制解析 OGNL
<body>
<h1>% 號的用法</h1>
<%
request.setAttribute("name","王東");
%>
<s:property value="#request.name" />
姓名:<s:textfield name="name" value="%{#request.name}" />
</body>
2)強制不解析 OGNL(沒用)
<body>
<h1>% 號的用法</h1>
<%
request.setAttribute("name","王東");
%>
<s:property value="#request.name" />
<s:property value="%{'#request.name'}" /> <!-- 不想被解析成OGNL表達式 -->
</body>
3、$ 號
$
號用在配置文件中使用 OGNL 表達式。
1)屬性文件
比如在國際化的時候:
-
message_zh_CN.properties
user.login=登錄, user.welcome=歡迎,${#session.user.username}
-
message_en_US.properties
user.login=Login, user.welcome=Welcome,${#session.user.username}\
2)XML 文件
比如文件下載時使用 OGNL 表達式語言。
<action name="download" class="xxx.DownloadAction">
<result type="stream">
<param name="Content-Type">文件類型</param>
<param name="Content-Disposition">attachment;filename=${文件名}</param>
</result>
</action>