`
zyongking
  • 浏览: 57366 次
  • 性别: Icon_minigender_1
  • 来自: 湖北
社区版块
存档分类
最新评论

ognl详解

阅读更多

该文章转载于:http://book.csdn.net/bookfiles/727/10072722465.shtml

 

1  业务控制器

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类型元素。在配置文件中增加了相应的配置,代码如下:

<action name="OgnlAction" class="ch8.OgnlAction">
        <result name="success">/showognl.jsp</result>
</action>

 

 2  JSP视图  

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

<%@ 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表达式的值。如下面的代码所示:

<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表达式,例如下面的代码片断所示:

<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 -->
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics