ognl详解

1  业务控制器

Java代码
  1. import java.util.Date;  
  2.   
  3. import java.util.LinkedList;  
  4.   
  5. import java.util.List;  
  6.   
  7. import javax.servlet.http.HttpServletRequest;  
  8.   
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import javax.servlet.http.HttpSession;  
  12.   
  13. import org.apache.struts2.ServletActionContext;  
  14.   
  15. import com.opensymphony.xwork2.ActionContext;  
  16.   
  17. import com.opensymphony.xwork2.ActionSupport;  
  18.   
  19. public class OgnlAction extends ActionSupport {  
  20.   
  21.     // List类型属性  
  22.   
  23.     private List<Person> persons;  
  24.   
  25.     // execute方法  
  26.   
  27.     public String execute() throws Exception {  
  28.   
  29.         // 获得ActionContext实例,以便访问Servlet API  
  30.   
  31.         ActionContext ctx = ActionContext.getContext();  
  32.   
  33.         // 存入application  
  34.   
  35.         ctx.getApplication().put("msg", "application信息");  
  36.   
  37.         // 保存session  
  38.   
  39.         ctx.getSession().put("msg", "seesion信息");  
  40.   
  41.         // 保存request信息  
  42.   
  43.         HttpServletRequest request = ServletActionContext.getRequest();  
  44.   
  45.         request.setAttribute("msg", "request信息");  
  46.   
  47.         // 为persons赋值  
  48.   
  49.         persons = new LinkedList<Person>();  
  50.   
  51.         Person person1 = new Person();  
  52.   
  53.         person1.setName("pla1");  
  54.   
  55.         person1.setAge(26);  
  56.   
  57.         person1.setBirthday(new Date());  
  58.   
  59.         persons.add(person1);  
  60.   
  61.         Person person2 = new Person();  
  62.   
  63.         person2.setName("pla2");  
  64.   
  65.         person2.setAge(36);  
  66.   
  67.         person2.setBirthday(new Date());  
  68.   
  69.         persons.add(person2);  
  70.   
  71.         Person person3 = new Person();  
  72.   
  73.         person3.setName("pla3");  
  74.   
  75.         person3.setAge(16);  
  76.   
  77.         person3.setBirthday(new Date());  
  78.   
  79.         persons.add(person3);  
  80.   
  81.         return SUCCESS;  
  82.   
  83.     }  
  84.   
  85.     public List<Person> getPersons() {  
  86.   
  87.         return persons;  
  88.   
  89.     }  
  90.   
  91.     public void setPersons(List<Person> persons) {  
  92.   
  93.         this.persons = persons;  
  94.   
  95.     }  
  96.   
  97. }  
import java.util.Date;  import java.util.LinkedList;  import java.util.List;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;  import javax.servlet.http.HttpSession;  import org.apache.struts2.ServletActionContext;  import com.opensymphony.xwork2.ActionContext;  import com.opensymphony.xwork2.ActionSupport;  public class OgnlAction extends ActionSupport {  	// List类型属性  	private List<Person> persons;  	// execute方法  	public String execute() throws Exception {  		// 获得ActionContext实例,以便访问Servlet API  		ActionContext ctx = ActionContext.getContext();  		// 存入application  		ctx.getApplication().put("msg", "application信息");  		// 保存session  		ctx.getSession().put("msg", "seesion信息");  		// 保存request信息  		HttpServletRequest request = ServletActionContext.getRequest();  		request.setAttribute("msg", "request信息");  		// 为persons赋值  		persons = new LinkedList<Person>();  		Person person1 = new Person();  		person1.setName("pla1");  		person1.setAge(26);  		person1.setBirthday(new Date());  		persons.add(person1);  		Person person2 = new Person();  		person2.setName("pla2");  		person2.setAge(36);  		person2.setBirthday(new Date());  		persons.add(person2);  		Person person3 = new Person();  		person3.setName("pla3");  		person3.setAge(16);  		person3.setBirthday(new Date());  		persons.add(person3);  		return SUCCESS;  	}  	public List<Person> getPersons() {  		return persons;  	}  	public void setPersons(List<Person> persons) {  		this.persons = persons;  	}  }

该业务控制器分别在application、session和request中存入名为“msg”的字符串信息,另外定义了一个List类型属性,同时添加了两个Person类型元素。在配置文件中增加了相应的配置,代码如下:

Xml代码
  1. <action name="OgnlAction" class="ch8.OgnlAction">  
  2.         <result name="success">/showognl.jsp</result>  
  3. </action>  
<action name="OgnlAction" class="ch8.OgnlAction">         <result name="success">/showognl.jsp</result> </action>  

 

 2  JSP视图  

showognl.jsp是使用了OGNL表达式的JSP视图,视图用来显示Action中处理的各种信息,读者可以看到,使用OGNL表达式,代码更加简洁和直观.

Html代码
  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>  
  2.   
  3. <%@ taglib prefix="s" uri="/struts-tags" %>  
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/ xhtml1/DTD/xhtml1-transitional.dtd">  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8.   
  9. <head>  
  10.   
  11.     <title>Struts2 OGNL 演示</title>  
  12.   
  13. </head>  
  14.   
  15. <body>      
  16.   
  17.     <h3>访问OGNL上下文和Action上下文</h3>  
  18.   
  19.     <!-使用OGNL访问属性值-->  
  20.   
  21.     <p>parameters: <s:property value="#parameters.msg" /></p>  
  22.   
  23.     <p>request.msg: <s:property value="#request.msg" /></p>  
  24.   
  25.     <p>session.msg: <s:property value="#session.msg" /></p>  
  26.   
  27.     <p>application.msg: <s:property value="#application.msg" /></p>  
  28.   
  29.     <p>attr.msg: <s:property value="#attr.msg" /></p>  
  30.   
  31.     <hr />  
  32.   
  33.     <h3>用于过滤和投影(projecting)集合</h3>  
  34.   
  35.     <p>年龄大于20</p>  
  36.   
  37.     <ul>  
  38.   
  39.     <!-判断年龄-->  
  40.   
  41.         <s:iterator value="persons.{?#this.age>20}">  
  42.   
  43.             <li><s:property value="name" /> - 年龄:<s:property value="age" /></li>  
  44.   
  45.         </s:iterator>  
  46.   
  47.     </ul>  
  48.   
  49.     <p>姓名为pla1的年龄: <s:property value="persons. {?#this.name=='pla1'} .{age}[0]"/></p>  
  50.   
  51.     <hr />  
  52.   
  53.     <h3>构造Map</h3>  
  54.   
  55.     <s:set name="foobar" value="#{'foo1':'bar1', 'foo2':'bar2'}" />  
  56.   
  57.     <p>The value of key "foo1" is <s:property value="#foobar['foo1']" /></p>  
  58.   
  59. </body>  
  60.   
  61. </html>  
<%@ 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>Struts2 OGNL 演示</title>  </head>  <body>          <h3>访问OGNL上下文和Action上下文</h3>      <!-使用OGNL访问属性值-->      <p>parameters: <s:property value="#parameters.msg" /></p>      <p>request.msg: <s:property value="#request.msg" /></p>      <p>session.msg: <s:property value="#session.msg" /></p>      <p>application.msg: <s:property value="#application.msg" /></p>      <p>attr.msg: <s:property value="#attr.msg" /></p>      <hr />      <h3>用于过滤和投影(projecting)集合</h3>      <p>年龄大于20</p>      <ul>      <!-判断年龄-->          <s:iterator value="persons.{?#this.age>20}">              <li><s:property value="name" /> - 年龄:<s:property value="age" /></li>          </s:iterator>      </ul>      <p>姓名为pla1的年龄: <s:property value="persons. {?#this.name=='pla1'} .{age}[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>  

 

 

OGNL中的#、%和$符号

#、%和$符号在OGNL表达式中经常出现,而这三种符号也是开发者不容易掌握和理解的部分。在这里笔者简单介绍它们的相应用途。

1.#符号

#符号的用途一般有三种。

—    访问非根对象属性,例如示例中的#session.msg表达式,由于Struts 2中值栈被视为根对象,所以访问其他非根对象时,需要加#前缀。实际上,#相当于ActionContext. getContext();#session.msg表达式相当于ActionContext.getContext().getSession(). getAttribute("msg") 。

—    用于过滤和投影(projecting)集合,如示例中的persons.{?#this.age>20}。

—    用来构造Map,例如示例中的#{'foo1':'bar1', 'foo2':'bar2'}。

2.%符号

%符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值。如下面的代码所示:

Html代码
  1. <h3>构造Map</h3>  
  2.   
  3.     <s:set name="foobar" value="#{'foo1':'bar1', 'foo2':'bar2'}" />  
  4.   
  5.     <p>The value of key "foo1" is <s:property value="#foobar['foo1']" /></p>  
  6.   
  7.     <p>不使用%:<s:url value="#foobar['foo1']" /></p>  
  8.   
  9.     <p>使用%:<s:url value="%{#foobar['foo1']}" /></p>  
<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>      <p>不使用%:<s:url value="#foobar['foo1']" /></p>      <p>使用%:<s:url value="%{#foobar['foo1']}" /></p>

3.$符号

$符号主要有两个方面的用途。

—    在国际化资源文件中,引用OGNL表达式,例如国际化资源文件中的代码:reg.agerange=国际化资源信息:年龄必须在${min}同${max}之间。

—    在Struts 2框架的配置文件中引用OGNL表达式,例如下面的代码片断所示:

Xml代码
  1. <validators>  
  2.   
  3.     <field name="intb">  
  4.   
  5.             <field-validator type="int">  
  6.   
  7.             <param name="min">10</param>  
  8.   
  9.             <param name="max">100</param>  
  10.   
  11.             <message>BAction-test校验:数字必须为${min}为${max}之间!</message>  
  12.   
  13.         </field-validator>  
  14.   
  15.     </field>  
  16.   
  17. </validators>  
<validators>      <field name="intb">              <field-validator type="int">              <param name="min">10</param>              <param name="max">100</param>              <message>BAction-test校验:数字必须为${min}为${max}之间!</message>          </field-validator>      </field>  </validators> 

 

<!-- page -->
posted on 2011-01-19 18:31  sin  阅读(377)  评论(0编辑  收藏  举报

阳江人才网