Java Study Center |
|
|||
日歷
統計
導航常用鏈接留言簿(1)隨筆檔案(40)文章檔案(3)搜索最新評論
閱讀排行榜評論排行榜 |
本人是一個EL(Expression Language,以下譯為表達式語言)的支持者。因為我對<% %>寫法極為反感,忘記了在那本書上看到的一句話——“使用標志(Tag)的一個目的就是避免在JSP頁面中出現過多的<%%>的語句,使頁面與后臺代碼分離。” 表達式語言主要有以下幾大好處:
Struts 2中的表達式語言Struts 2支持以下幾種表達式語言:
Struts 2默認的表達式語言是OGNL,原因是它相對其它表達式語言具有下面幾大優勢:
OGNL的用法OGNL是通常要結合Struts 2的標志一起使用,如<s:property value="xx" />等。大家經常遇到的問題是#、%和$這三個符號的使用。下面我想通過例子講述這個問題: 首先新建名為Struts2_OGNL的Web工程,配置開發環境。之前很多朋友在使用Struts 2的過程中都遇到亂碼問題。當然亂碼問題由來已久,而且涉及多方面的知識,所以并非三言兩語可以說明白,而且互聯網上也已經有很多這方便的文章,大家可以Google一下。不過,如果你在開發的過程,多注意一下,避免亂碼問題也不難。亂碼多數是由于編碼與解碼所使用的方式不同造成的,所以我建議大家將編碼方式都設為“utf-8”,如<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>。另外,在配置web.xml時使用ActionContextCleanUp過濾器(Filter),如下面代碼所示: <?xml version="1.0" encoding="UTF-8"?>
清單1 WebContent/WEB-INF/web.xml
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts 2 OGNL</display-name> <filter> <filter-name>struts-cleanup</filter-name> <filter-class> org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> “#”主要有三種用途:
下面讓我們它們的具體寫法,首先是Action類代碼: ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 以上代碼分別在request、session和application的范圍內添加“userName”屬性,然后再在JSP頁面使用OGNL將其取回。我還創建了Book對象的列表用于演示“用于過濾和投影(projecting)集合”的功能,至于Book的代碼大家可以在我前一文章《在Struts 2中實現CRUD》看到。 下面是Ognl.jsp的代碼,內容如下: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
清單3 WebContent/Ognl.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Struts OGNL Demo</title> </head> <body> <h3>訪問OGNL上下文和Action上下文</h3> <p>parameters: <s:property value="#parameters.userName" /></p> <p>request.userName: <s:property value="#request.userName" /></p> <p>session.userName: <s:property value="#session.userName" /></p> <p>application.userName: <s:property value="#application.userName" /></p> <p>attr.userName: <s:property value="#attr.userName" /></p> <hr /> <h3>用于過濾和投影(projecting)集合</h3> <p>Books more than $35</p> <ul> <s:iterator value="books.{?#this.price > 35}"> <li><s:property value="title" /> - $<s:property value="price" /></li> </s:iterator> </ul> <p>The price of "Code Complete, Second Edition" is: <s:property value="books.{?#this.title=='Code Complete, Second Edition'}.{price}[0]"/></p> <hr /> <h3>構造Map</h3> <s:set name="foobar" value="#{'foo1':'bar1', 'foo2':'bar2'}" /> <p>The value of key "foo1" is <s:property value="#foobar['foo1']" /></p> </body> </html> 以上代碼值得注意的是“<s:property value="books.{?#this.title=='Code Complete, Second Edition'}.{price}[0]"/>”,因為“books.{?#this.title=='Code Complete, Second Edition'}.{price}”返回的值是集合類型,所以要用“[索引]”來訪問其值。 最后是Struts 2的配置文件struts.xml,內容如下: <?xml version="1.0" encoding="UTF-8"?>
清單4 src/struts.xml <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="Struts2_OGNL_DEMO" extends="struts-default"> <action name="Ognl" class="tutorial.action.OgnlAction"> <result>/Ognl.jsp</result> </action> </package> </struts> 發布運行應用程序,結果如下所示: “%”符號的用途是在標志的屬性為字符串類型時,計算OGNL表達式的值。例如在Ognl.jsp中加入以下代碼: <hr />
清單6 演示%用途的代碼片段
<h3>%的用途</h3> <p><s:url value="#foobar['foo1']" /></p> <p><s:url value="%{#foobar['foo1']}" /></p> 刷新頁面,結果如下所示:
“$”有兩個主要的用途
總結OGNL是一種功能很大的表達式語言,熟悉它可以使我們的開發變得更快捷。 |
![]() |
|
Copyright © 綠茶_鄭州 | Powered by: 博客園 模板提供:滬江博客 |