歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts 2中的表達式語言

Struts 2中的表達式語言

日期:2017/3/1 10:22:36   编辑:Linux編程

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),如下面代碼所示:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_9" version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6. <display-name>Struts 2 OGNL</display-name>
  7. <filter>
  8. <filter-name>struts-cleanup</filter-name>
  9. <filter-class>
  10. org.apache.struts2.dispatcher.ActionContextCleanUp
  11. </filter-class>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>struts-cleanup</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>
  17. <filter>
  18. <filter-name>struts2</filter-name>
  19. <filter-class>
  20. org.apache.struts2.dispatcher.FilterDispatcher
  21. </filter-class>
  22. </filter>
  23. <filter-mapping>
  24. <filter-name>struts2</filter-name>
  25. <url-pattern>/*</url-pattern>
  26. </filter-mapping>
  27. <welcome-file-list>
  28. <welcome-file>index.html</welcome-file>
  29. </welcome-file-list>
  30. </web-app>

“#”主要有三種用途:

  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'}。
Copyright © Linux教程網 All Rights Reserved