Struts2學習——1400OGNL2訪問方法與屬性

OGNL2訪問方法與屬性

在上一小節中,我們訪問了值棧中action的屬性,值棧中對象的屬性。在本小節中將訪問值棧中action的屬性與方法,值棧中對象的屬性與方法,還有靜態屬性與方法。

1.訪問普通屬性值及方法

爲了更好的做實驗,重新定義了兩個class,Cat和Dog,代碼如下所示。

Cat

package com.bjsxt.struts2.ognl;

public class Cat {
    private Dog friend;
    private String name;
    public String eat(){
        return "cat eating...";
    }
    public Dog getFriend() {
        return friend;
    }
    public void setFriend(Dog friend) {
        this.friend = friend;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

易得,Cat中有一個成員變量,類型爲Dog,name=friend;有成員變量 name,有方法eat()。

Dog

package com.bjsxt.struts2.ognl;

public class Dog {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Dog中只有一個成員變量name。

同時,也對OgnlAction進行了一些添加

package com.bjsxt.struts2.ognl;


import com.opensymphony.xwork2.ActionSupport;

public class OgnlAction extends ActionSupport {

    private Cat cat;
    private String password;
    private User user;
    private String username;

    public String r_str(){
        return "action_str";
    }

    public String execute() {
        return SUCCESS;
    }
    public Cat getCat() {
        return cat;
    }
    public String getPassword() {
        return password;
    }
    public User getUser() {
        return user;
    }
    public String getUsername() {
        return username;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public void setUsername(String username) {
        this.username = username;
    }
}

可以看到添加了一個成員變量Cat cat,還有一個方法r_str();

接下來,在ognl.jsp中修改,取出值棧中的Action或者是對象的成員變量、方法。

ognl.jsp(增加的部分代碼)

<li>訪問值棧中的對象的普通屬性: <s:property value="cat.friend.name"/></li>
<li>訪問值棧中的對象的普通方法: <s:property value="password.length()"/></li>
<li>訪問值棧中的對象的普通方法: <s:property value="cat.eat()"/></li>
<li>訪問值棧中的對象的普通方法: <s:property value="r_str()"/></li>

得到的結果,如下圖
這裏寫圖片描述

其實ognl說到底,就是代碼中,value=之後的那部分,主要是一些規則,用以取出變量值以及方法。

2. 訪問靜態屬性及方法

爲了做這個實驗,定義了一個新的class

Static_Example

package com.bjsxt.struts2.ognl;

public class Static_Example {
    public static String static_value="STATIC_VALUE";
    public static String r_static_str(){
        return "static method()";
    }
}

裏面有一個靜態變量,一個靜態方法。

在ognl.jsp中添加相應的ognl語句

<li>訪問靜態方法: <s:property value="@com.bjsxt.struts2.ognl.Static_Example@r_static_str()"/></li>
<li>訪問靜態屬性: <s:property value="@com.bjsxt.struts2.ognl.Static_Example@static_value"/></li>
<li>訪問Math類型靜態屬性: <s:property value="@@max(2,3)"/></li>

得到結果,如下圖所示:

這裏寫圖片描述

從結果中,可以看出,要取出靜態方法或是靜態屬性,ognl規則爲

@完整類名@方法名或屬性名

獲取靜態math類型靜態屬性

@@Math表達式

【附】ognl.jsp(全)

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>OGNL表達式語言學習</title>
</head>
<body>
    <ol>
        <li>訪問值棧中的action的普通屬性: username = <s:property value="username"/> </li>
        <li>訪問值棧中對象的普通屬性(get set方法):<s:property value="user.age"/> | <s:property value="user['age']"/> | <s:property value="user[\"age\"]"/> | wrong: <%--<s:property value="user[age]"/>--%></li>
        <li>訪問值棧中的對象的普通屬性: <s:property value="cat.friend.name"/></li>
        <li>訪問值棧中的對象的普通方法: <s:property value="password.length()"/></li>
        <li>訪問值棧中的對象的普通方法: <s:property value="cat.eat()"/></li>
        <li>訪問值棧中的對象的普通方法: <s:property value="r_str()"/></li>
        <hr/>
        <li>訪問靜態方法: <s:property value="@com.bjsxt.struts2.ognl.Static_Example@r_static_str()"/></li>
        <li>訪問靜態屬性: <s:property value="@com.bjsxt.struts2.ognl.Static_Example@static_value"/></li>
        <li>訪問Math類型靜態屬性: <s:property value="@@max(2,3)"/></li>
        <hr/>
    </ol>

    <s:debug></s:debug>
</body>
</html>

若有不足之處,請不吝賜教

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