首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

OGNL中的#%,$ 三种符号

2012-09-27 
OGNL中的#,%,$ 三种符号?OGNL的用法OGNL是通常要结合Struts 2的标志一起使用,如s:property valuexx /

OGNL中的#,%,$ 三种符号

?OGNL的用法

OGNL是通常要结合Struts 2的标志一起使用,如<s:property value="xx" />等。大家经常遇到的问题是#、%和$这三个符号的使用。下面我想通过例子讲述这个问题:

“#”主要有三种用途:

?名称

作用

例子

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属性,直到找到为止

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; }}

<s:set name="myMap" value="#{'key1':'value1','key2':'value2'}"/> <s:property value="#myMap['key1']"/> <s:url value="#myMap['key1']" />

??????????? <result type="redirect">ListPhotos.action?albumId=${albumId}</result>
</action>

?

热点排行