Struts2框架學習之OGNL表達式

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章