struts2 ognl 表達式
Struts 2中的表達式語言
Struts 2支持以下幾種表達式語言:
1. OGNL(Object-Graph Navigation Language),可以方便地操作對象屬性的開源表達式語言;
2. JSTL(JSP Standard Tag Library),JSP 2.0集成的標準的表達式語言;
3. Groovy,基於Java平臺的動態語言,它具有時下比較流行的動態語言(如Python、Ruby和Smarttalk等)的一些起特性;
4. Velocity,嚴格來說不是表達式語言,它是一種基於Java的模板匹配引擎,具說其性能要比JSP好。
Struts 2默認的表達式語言是OGNL,原因是它相對其它表達式語言具有下面幾大優勢:
1. 支持對象方法調用,如xxx.doSomeSpecial();
2. 支持類靜態的方法調用和值訪問,表達式的格式爲@[類全名(包括包路徑)]@[方法名 | 值名],例如:@java.lang.String@format('foo %s', 'bar')或@tutorial.MyConstant@APP_NAME;
3. 支持賦值操作和表達式串聯,如price=100, discount=0.8, calculatePrice(),這個表達式會返回80;
4. 訪問OGNL上下文(OGNL context)和ActionContext;
5. 操作集合對象。
“#”主要有三種用途:
1. 訪問OGNL上下文和Action上下文,#相當於ActionContext.getContext();下面有幾個ActionContext中有用的屬性:
parameters
包含當前HTTP請求參數的Map
#parameters.id[0]作用相當於request.getParameter("id")
request
包含當前HttpServletRequest的屬性(attribute)的Map
#request.userName相當於request.getAttribute("userName")
session
包含當前HttpSession的屬性(attribute)的Map
#session.userName相當於session.getAttribute("userName")
application
包含當前應用的ServletContext的屬性(attribute)的Map
#application.userName相當於application.getAttribute("userName")
attr
用於按request > session > application順序訪問其屬性(attribute)
#attr.userName相當於按順序在以上三個範圍(scope)內讀取userName屬性,直到找到爲止
2. 用於過濾和投影(projecting)集合,如books.{?#this.price<100};
<s:iterator value="books.{?#this.price > 35}">
<li><s:property value="title" /> - $<s:property value="price" /></li>
</s:iterator>
<s:property value="books.{?#this.title=='Code Complete, Second Edition'}.{price}[0]"/>
問題:?#this的多個判斷問題 &&
3. 構造Map,如#{'foo1':'bar1', 'foo2':'bar2'}。 之前講過
%的特性 計算表達式 類似javascript中的eval函數 可用s:url舉例說明%用法
“%”符號的用途是在標誌的屬性爲字符串類型時,計算OGNL表達式的值
舉例:用s:url或<s:set舉例
<s:textfield key="state.label" name="state" value="%{'CA'}" />
計算boolean值(帶有轉義符的):
<s:select key="state.label" name="state" multiple="%{true}"/>
帶有屬性的:
<s:select key="state.label" name="state" multiple="allowMultiple"/>
既帶有轉義符又帶有屬性的:
<s:select key="state.label" name="state" multiple="%{allowMultiple}"/>
表示式語言符號
1.在Freemarker、Velocity或者JSTL的表達式語言的JavaBean對象的標準文本
<p>Username: ${user.username}</p>
2.在值棧中的一個username屬性
<s:textfield name="username"/>
3. 引用值棧中的屬性的另一種方式
<s:url id="es" action="Hello">
<s:param name="request_locale">
es
</s:param>
</s:url>
<s:a href="%{es}">Espanol</s:a>
4. 在Session Context中獲得user對象的userName屬性
<s:property name="#session.user.username" />
5. 在一個靜態map中,像("username","trillian")一樣
<s:select label="FooBar" name="foo" list="#{'username':'trillian', 'username':'zaphod'}" />
$”有兩個主要的用途
1. 用於在國際化資源文件中,引用OGNL表達式,參考前面的國際化校驗配置
2. 在Struts 2配置文件中,引用OGNL表達式傳遞參數
<action name="AddPhoto" class="addPhoto">
<interceptor-ref name="fileUploadStack" />
<result type="redirect">ListPhotos.action?albumId=${albumId}</result>
</action>
Struts 2支持以下幾種表達式語言:
1. OGNL(Object-Graph Navigation Language),可以方便地操作對象屬性的開源表達式語言;
2. JSTL(JSP Standard Tag Library),JSP 2.0集成的標準的表達式語言;
3. Groovy,基於Java平臺的動態語言,它具有時下比較流行的動態語言(如Python、Ruby和Smarttalk等)的一些起特性;
4. Velocity,嚴格來說不是表達式語言,它是一種基於Java的模板匹配引擎,具說其性能要比JSP好。
Struts 2默認的表達式語言是OGNL,原因是它相對其它表達式語言具有下面幾大優勢:
1. 支持對象方法調用,如xxx.doSomeSpecial();
2. 支持類靜態的方法調用和值訪問,表達式的格式爲@[類全名(包括包路徑)]@[方法名 | 值名],例如:@java.lang.String@format('foo %s', 'bar')或@tutorial.MyConstant@APP_NAME;
3. 支持賦值操作和表達式串聯,如price=100, discount=0.8, calculatePrice(),這個表達式會返回80;
4. 訪問OGNL上下文(OGNL context)和ActionContext;
5. 操作集合對象。
“#”主要有三種用途:
1. 訪問OGNL上下文和Action上下文,#相當於ActionContext.getContext();下面有幾個ActionContext中有用的屬性:
parameters
包含當前HTTP請求參數的Map
#parameters.id[0]作用相當於request.getParameter("id")
request
包含當前HttpServletRequest的屬性(attribute)的Map
#request.userName相當於request.getAttribute("userName")
session
包含當前HttpSession的屬性(attribute)的Map
#session.userName相當於session.getAttribute("userName")
application
包含當前應用的ServletContext的屬性(attribute)的Map
#application.userName相當於application.getAttribute("userName")
attr
用於按request > session > application順序訪問其屬性(attribute)
#attr.userName相當於按順序在以上三個範圍(scope)內讀取userName屬性,直到找到爲止
2. 用於過濾和投影(projecting)集合,如books.{?#this.price<100};
<s:iterator value="books.{?#this.price > 35}">
<li><s:property value="title" /> - $<s:property value="price" /></li>
</s:iterator>
<s:property value="books.{?#this.title=='Code Complete, Second Edition'}.{price}[0]"/>
問題:?#this的多個判斷問題 &&
3. 構造Map,如#{'foo1':'bar1', 'foo2':'bar2'}。 之前講過
%的特性 計算表達式 類似javascript中的eval函數 可用s:url舉例說明%用法
“%”符號的用途是在標誌的屬性爲字符串類型時,計算OGNL表達式的值
舉例:用s:url或<s:set舉例
<s:textfield key="state.label" name="state" value="%{'CA'}" />
計算boolean值(帶有轉義符的):
<s:select key="state.label" name="state" multiple="%{true}"/>
帶有屬性的:
<s:select key="state.label" name="state" multiple="allowMultiple"/>
既帶有轉義符又帶有屬性的:
<s:select key="state.label" name="state" multiple="%{allowMultiple}"/>
表示式語言符號
1.在Freemarker、Velocity或者JSTL的表達式語言的JavaBean對象的標準文本
<p>Username: ${user.username}</p>
2.在值棧中的一個username屬性
<s:textfield name="username"/>
3. 引用值棧中的屬性的另一種方式
<s:url id="es" action="Hello">
<s:param name="request_locale">
es
</s:param>
</s:url>
<s:a href="%{es}">Espanol</s:a>
4. 在Session Context中獲得user對象的userName屬性
<s:property name="#session.user.username" />
5. 在一個靜態map中,像("username","trillian")一樣
<s:select label="FooBar" name="foo" list="#{'username':'trillian', 'username':'zaphod'}" />
$”有兩個主要的用途
1. 用於在國際化資源文件中,引用OGNL表達式,參考前面的國際化校驗配置
2. 在Struts 2配置文件中,引用OGNL表達式傳遞參數
<action name="AddPhoto" class="addPhoto">
<interceptor-ref name="fileUploadStack" />
<result type="redirect">ListPhotos.action?albumId=${albumId}</result>
</action>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.