Struts 2中的表达式语言
Struts 2支持以下几种表达式语言:
- OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言;
- JSTL(JSP Standard Tag Library),JSP 2.0集成的标准的表达式语言;
- Groovy,基于Java平台的动态语言,它具有时下比较流行的动态语言(如Python、Ruby和Smarttalk等)的一些起特性;
- Velocity,严格来说不是表达式语言,它是一种基于Java的模板匹配引擎,具说其性能要比JSP好。
OGNL的用法
OGNL是通常要结合Struts 2的标志一起使用,如<s:property value="xx" />等。大家经常遇到的问题是#、%和$这三个符号的使用。下面我想通过例子讲述这个问题:
“#”主要有三种用途:
- 访问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属性,直到找到为止 |
- 用于过滤和投影(projecting)集合,如books.{?#this.price<100};
- 构造Map,如#{'foo1':'bar1', 'foo2':'bar2'}。
下面让我们它们的具体写法,首先是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 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;
- }
- }
“%”符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值。例如在Ognl.jsp中加入以下代码:
<h3>%的用途</h3>
<p><s:url value="#foobar['foo1']" /></p>
<p><s:url value="%{#foobar['foo1']}" /></p>
“$”有两个主要的用途
- 用于在国际化资源文件中,引用OGNL表达式,例子请参考《在Struts 2.0中国际化(i18n)您的应用程序》
- 在Struts 2配置文件中,引用OGNL表达式,如
<action name="AddPhoto" class="addPhoto">
<interceptor-ref name="fileUploadStack" />
<result type="redirect">ListPhotos.action?albumId=${albumId}</result>
</action>