OGNL(Object Grsphic Navigation Language,对象图导航语言)是Struts2默认的表达式语言。和EL表达式语言相比,具有以下优点:
①支持对象方法的调用;
②支持静态方法调用和值访问,格式为@类全名(包括包路径)@方法名或者值名称;
③操作集合对象。
OGNL有一个上下文context的概念,其实为一个Map结构,实现了Map接口。在Struts2中context的实现为ActionContext。另OGNL表达式需要结合struts的标签实现。
1、OGNL表达式调用方法
①对象方法的调用
对象方法调用使用<s:property/>
标签实现,其中value属性值直接写action中方法的名称()即可,如有参数传递参数即可。
OGNLService.java处理类如下:
package com.struts.service;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
/**
* @TODO OGNL表达式
* @author Administrator
* @date 2015年7月1日 下午10:43:12
* @version 1.0
*/
@SuppressWarnings("serial")
public class OGNLService extends ActionSupport{
@Override
public String execute(){
return Action.SUCCESS;
}
public void deal(){
System.out.println("deal");
}
public void deal(String param){
System.out.println(param);
}
}
需要在struts-config.xml中引入struts-ognl.xml,引入方法如下:
<!-- ognl表达式的配置文件 -->
<include file="struts-ognl.xml"></include>
struts-ognl.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="ognl" extends="struts-default">
<action name="ognl" class="com.struts.service.OGNLService">
<result name="success">/ognl.jsp</result>
</action>
</package>
</struts>
ognl.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>OGNL表达式</title>
</head>
<body>
<s:debug/>
<!-- 对象方法的调用 -->
对象方法的调用:<s:property value="get()"/><br/>
<!-- 对象方法参数的传递 -->
对象方法(带参数)的调用:<s:property value="deal('对象方法参数')"/><br/>
</body>
</html>
②静态方法调用和值访问
struts2默认jsp页面不支持OGNL的静态方法调用,因此需要在jsp页面支持对静态方法的调用,使用常量可以解决此问题,在struts-ognl.xml中配置常量:
<!-- 支持页面使用静态方法 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
页面通过OGNL访问静态方法和静态值代码如下:
<!-- 静态方法访问和值访问 -->
静态方法访问:<s:property value="@java.lang.Math@abs(-10)"/><br/>
静态方法访问:<s:property value="@@abs(-10)"/><br/>
静态值访问:<s:property value="@org.apache.struts2.StrutsConstants@STRUTS_DEVMODE"/><br/>
③构造方法的调用
构造方法代码调用如下:
<!-- 构造方法的调用 -->
构造方法调用:<s:property value="new com.struts.controller.Login('张三').username"/>
③使用#符号标注命名空间
使用#符号可以获取application、session、request、parameters、attr范围内的属性值,其中通过action向前台页面传递值,可以使用ServletActionContext进行存值。
修改OGNLService.java,使用ServletActionContext存放不同范围内的值。
//添加application范围内属性值
ServletActionContext.getServletContext().setAttribute("username", "application_username");
//添加session范围内的属性值
ServletActionContext.getRequest().getSession().setAttribute("username", "session_username");
//添加request范围内的属性值
ServletActionContext.getRequest().setAttribute("username", "request_username");
修改struts-ognl.xml配置文件,向jsp页面传递参数.
<package name="ognl" extends="struts-default">
<action name="ognl" class="com.struts.service.OGNLService">
<!-- 向前台页面传递参数username -->
<result name="success">/ognl.jsp?username=zhangsan</result>
</action>
</package>
jsp页面获取如下:
<!--使用#符号 -->
获取application范围:<s:property value="#application.username"/><br/>
获取session范围:<s:property value="#session.username"/><br/>
获取request范围:<s:property value="#request.username"/><br/>
获取parameters范围:<s:property value="#parameters.username"/><br/>
获取attr范围:<s:property value="#attr.username"/><br/>
其中attr的取值范围从page–>request–>session–>application的范围内取值。
④获取list或者map中的值
<!-- 使用OGNL表达式生成List/Map集合 -->
<!--list中存放的为字符串,则不需要使用#进行输出-->
<s:set name="list" value='{"1","a","3"}'/>
<s:iterator value="#list" id="item">
<s:property value="item"/>
</s:iterator><br/>
<!--list1中存放的为数值类型,则需要使用#进行输出-->
<s:set name="list1" value="{1,2,3}"/>
<s:iterator value="#list1" id="item">
<s:property value="#item"/>
</s:iterator><br/>
<s:set name="map" value="#{'1':'username','2':'password'}"/>
<s:iterator value="#map" var="item">
<s:property value="#item.key"/>--><s:property value="#item.value"/><br/>
<s:property value="key"/>--><s:property value="value"/><br/>
</s:iterator>
以上需要注意一点:在使用list转换时,如果list中的元素不是字符串类型的,则需要在在引用时加上#才能正确输出。另外生成map需要在value中使用#符号。
⑤判断对象是否在集合内
对于集合类型,OGNL表达式使用in和not in来判断。
<!-- in和 not in的使用 -->
<s:if test="'1' in {'1','2'}">
在
</s:if>
<s:else>
不在
</s:else><br/>
<s:if test="'1' not in {1,2}">
不在
</s:if>
<s:else>
在
</s:else><br/>
⑥投影功能
OGNL使用某个规则获取集合对象的子集。?:获取所有符合逻辑的元素;^:获取所有符合逻辑的第一个元素;$:获取所有符合逻辑的最后一个元素。
<!-- 投影功能的使用 -->
<!--使用?获取所有符合条件的 -->
<s:iterator value="lists.{?#this.age > 19}">
<s:property value="username"/>--><s:property value="age"/><br/>
</s:iterator>
<!--使用^获取符合条件的第一个 -->
<s:iterator value="lists.{^#this.age > 19}">
<s:property value="username"/>--><s:property value="age"/><br/>
</s:iterator>
<!--使用$获取符合条件的最后一个 -->
<s:iterator value="lists.{$#this.age > 19}">
<s:property value="username"/>--><s:property value="age"/><br/>
</s:iterator>
⑦%符号的使用
%类似于JavaScript中的evel()函数,%表明{}中的为ognl表达式。
<!-- %类似于js的evel函数 -->
<s:set value="#{'1':'www.baidu.com','2':'www.taobao.com' }" name="website"/>
<s:property value="#website['1']"/><br/>
<s:url value="#website['2']"/><br/>
<s:url value="%{#website['2']}"/><br/>
上述代码中,<s:url value="#website['2']"/>
输出为#website[‘2’],<s:url value="%{#website['2']}"/><br/>
输出为www.taobao.com。
⑧$的使用
在struts-ognl.xml中配置文件中获取action中的值。
<package name="ognl" extends="struts-default">
<action name="ognl" class="com.struts.service.OGNLService">
<!-- 向前台页面传递参数username -->
<result name="success">/ognl.jsp?message=${message}</result>
</action>
</package>
此外需要在OGNLService.java中增加message的String类型的变量,并赋值。
前台页面获取如下:
<!-- $的 使用-->
<s:property value="#parameters.message"/>-->${message }</br>