Ognl方法使用(利用表达式语言获取数据)

WebWork的EL-对象图导航语言(Object Graph Navigation Language, 缩写为OGNL)是作为Web页面脚本的最佳选择。

一. 基本特性

1. 访问bean属性

根据JavaBean的规范,bean属性使用诸如getXxx(), setXxx(), isXxx()或者hasXxx()这样标准形式的getter方法或
setter方法。其中, isXxx()和hasXxx()形式用于boolean属性。在WebWork中,访问这些属性(不管是获取数据还是
设置数据)都使用形式为xxx的属性引用。

示例:

a. action

package ch8;

import ch4.User;
import com.opensymphony.xwork.ActionSupport;

public class OgnlTest extends ActionSupport{
private User user;

public String execute() {
user = new User();
user.setName("zs");
user.setPassword("password");

return SUCCESS;
}
//setter和getter方法
}

b. result

<action name="ognlTest" class="ch8.OgnlTest">
<result name="success">ognl.jsp</result>
</action>

c. jsp

<%@page contentType="text/html;charset=UTF-8" %>
<%@taglib uri="webwork" prefix="ww" %>
<html>
<body>
<ww:property value="user.name"/> <!--其中value中内容为ognl表达式-->
<ww:property value="user.password"/>
</body>
</html>

2. 常量

OGNL表达式常量与EL类型,

常量类型 范例
------------ --------------
char 'a'
String "hello world"
boolean true false

另String常量可以使用单引号或双引号括起来,示例:

<ww:property value="\"a\""/>并不等同于 <ww:property value='a'/>

posted on 2013-02-16 19:31  蜜雪薇琪  阅读(580)  评论(0编辑  收藏  举报