亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

struts2中OGNL和 ValueStack(二)

系統 1893 0

表達式語言主要有以下幾大好處:

  1. 避免 (MyType) request.getAttribute() myBean.getMyProperty() 之類的語句,使頁面更簡潔;
  2. 支持運算符(如+-*/),比普通的標志具有更高的自由度和更強的功能;
  3. 簡單明了地表達代碼邏輯,使用代碼更可讀與便于維護。
  4. 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. 操作集合對象。

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" ?>
< 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 >

清單1 WebContent/WEB-INF/web.xml

?

“#”主要有三種用途:

  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} ;
  3. 構造Map,如 #{'foo1':'bar1', 'foo2':'bar2'} 。

下面讓我們它們的具體寫法,首先是Action類代碼:

package tutorial.action;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache. struts2 .interceptor.ServletRequestAware;
import org.apache. struts2 .interceptor.SessionAware;
import org.apache. struts2 .util.ServletContextAware;

import tutorial.model.Book;

import com.opensymphony.xwork2.ActionSupport;

public class OgnlAction extends ActionSupport implements ServletRequestAware, SessionAware, ServletContextAware ? {
? ?
private static final long serialVersionUID = 1L ;
? ?
? ?
private HttpServletRequest request;
? ?
private Map < String, String > session;
? ?
private ServletContext application;
? ?
private List < Book > books;
? ? ? ? ? ?
? ?
public void setServletRequest(HttpServletRequest request) {
? ? ? ?
this .request = request; ? ?
? ?}


? ?@SuppressWarnings(
" unchecked " )
? ?
public void setSession(Map session) {
? ? ? ?
this .session = session; ? ? ? ?
? ?}


? ?
public void setServletContext(ServletContext application) {
? ? ? ?
this .application = application;
? ?}

? ?
? ?
public List < Book > getBooks() {
? ? ? ?
return books;
? ?}


? ?@Override
? ?
public String execute() {
? ? ? ?request.setAttribute(
" userName " , " Max From request " );
? ? ? ?session.put(
" userName " , " Max From session " );
? ? ? ?application.setAttribute(
" userName " , " Max From application " );
? ? ? ?
? ? ? ?books
= new LinkedList < Book > ();
? ? ? ?books.add(
new Book( " 978-0735619678 " , " Code Complete, Second Edition " , 32.99 ));
? ? ? ?books.add(
new Book( " 978-0596007867 " , " The Art of Project Management " , 35.96 ));
? ? ? ?books.add(
new Book( " 978-0201633610 " , " Design Patterns: Elements of Reusable Object-Oriented Software " , 43.19 ));
? ? ? ?books.add(
new Book( " 978-0596527341 " , " Information Architecture for the World Wide Web: Designing Large-Scale Web Sites " , 25.19 ));
? ? ? ?books.add(
new Book( " 978-0735605350 " , " Software Estimation: Demystifying the Black Art " , 25.19 ));
? ? ? ?
? ? ? ?
return SUCCESS;
? ?}

}

清單2 src/tutorial/action/OgnlAction.java

以上代碼分別在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 " %>
<% @ 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 >

清單3 WebContent/ Ognl .jsp

以上代碼值得注意的是“ <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" ?>

<! 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 >

清單4 src/struts.xml

發布運行應用程序,結果如下所示:

?

?

?

“%”符號的用途是在標志的屬性為字符串類型時,計算 OGNL 表達式的值。例如在 Ognl .jsp中加入以下代碼:

< hr />
? ?
< h3 > %的用途 </ h3 >
? ?
< p >< s:url value ="#foobar['foo1']" /></ p >
? ?
< p >< s:url value ="%{#foobar['foo1']}" /></ p >

清單6 演示%用途的代碼片段

?

?

?

struts2中OGNL和 ValueStack(二)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 免费福利入口在线观看 | 国产成人精品亚洲2020 | 欧美亚洲一区二区三区在线 | 久久黄色一级视频 | 桃花视频www | 日本一级毛片无遮挡 | 奇米影视777在线播放 | 五月天婷婷在线观看高清 | 精品99视频 | 日本玖玖 | 日韩亚洲欧美一区 | 99热久久精品最新 | 久久婷婷色 | 日本又黄又爽又色的免费视频 | 国产午夜精品一区二区三区 | 欧美一级视 | 日本视频久久 | 成人影院观看 | 牛牛色婷婷在线视频播放 | 免费观看欧美精品成人毛片能看的 | www.久久99| 久久一区视频 | 三级aaa黄特色 | 国产精品一区二区手机看片 | 欧美a视频 | www黄在线观看 | 亚洲欧美日韩精品高清 | 日韩免费视频观看 | 久久国产综合精品欧美 | 久久精品成人欧美大片免费 | 精品一久久香蕉国产线看观看下 | 日本三级带日本三级带黄首页 | 亚洲欧美日韩不卡一区二区三区 | 久久精品女人毛片国产 | 欧美天天性影院 | 久久精品国产国产精品四凭 | 久久精品a一国产成人免费网站 | 天天操天天噜 | 亚洲欧洲国产成人精品 | 久久综合久久自在自线精品自 | 青青青激情视频在线最新 |