Struts2-OGNL

在JSP頁面獲取數據除了使用EL表達式之外,Struts2還提供了另外一種訪問並顯示數據的表達式,那就是OGNL(Object-Graph Navigation Language 對象圖導航語言)。

  • OGNL表達式功能及特點
    1、能存取對象屬性、調用對象方法
    2、能訪問靜態方法、靜態屬性
    3、能訪問值棧及Stack Context
    4、能操作集合對象
    5、支付賦值、運算操作、字段類型轉化

一、訪問值棧

值棧(Value Stack)可以理解爲一個用於存儲數據的Map,主要用於存放當前Action實例。

  • action配置
<action name="gotoHousePage" class="com.pb05.demo01.StudentsAction" method="gotoHousePage">
    <result>/jsp/05_OGNL訪問值棧和StackContext/01_訪問值棧.jsp</result>
</action>
  • action類代碼(JavaBean方式爲例)
public class StudentsAction extends ActionSupport {
    private Students students;
    private String name;
    private int age = 18;

    public String gotoHousePage() {
        Students students = new Students("張三", "123");
        this.setStudents(students);
        this.name = "你好!";
        return SUCCESS;
    }

    public Students getStudents() {
        return students;
    }

    public void setStudents(Students students) {
        this.students = students;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}
  • 輸出頁面

這裏寫圖片描述

  • 通過action訪問的結果

這裏寫圖片描述


二、訪問Stack Context

上面的示例是訪問Action中的數據、OGNL同樣可以訪問Action以外的數據,如request、session、application等,這些數據都存儲在Stack Context中,注意,訪問Stack Context需要加上”#”

  • action配置
<action name="gotoHousePage2" class="com.pb05.demo02.StudentsAction" method="gotoHousePage">
    <result>/jsp/05_OGNL訪問值棧和StackContext/02_訪問StackContext.jsp</result>
</action>
  • action類代碼
public class StudentsAction extends ActionSupport {

    public String gotoHousePage() {
        ActionContext.getContext().getSession().put("uname", "張三");
        return SUCCESS;
    }

}
  • 輸出頁面
<body>
<s:property value="#session.uname"/>
</body>
  • 輸出結果

這裏寫圖片描述


三、訪問集合

演示使用OGNL訪問集合屬性

  • action配置
<action name="list_*" class="com.pb05.demo03.OgnlAction" method="{1}">
    <result>/jsp/05_OGNL訪問值棧和StackContext/03_訪問集合.jsp</result>
</action>
  • action類代碼
public class OgnlAction extends BaseAction {
    public String list() throws Exception {
        ArrayList<User> list = new ArrayList<User>();
        User u1 = new User("accp", "accp", "123");
        User u2 = new User("pb", "pb", "123");
        list.add(u1);
        list.add(u2);
        request.put("list", list);
        return SUCCESS;
    }

    public String map() throws Exception {
        User u1 = new User("accp", "accp", "123");
        User u2 = new User("pb", "pb", "123");
        HashMap<String, User> map = new HashMap<String, User>();
        map.put("m1", u1);
        map.put("m2", u2);
        request.put("map", map);
        return SUCCESS;
    }
}
  • 輸出頁面關鍵代碼
<body>
list集合中大小:
    <s:property value="#request.list.size()"/>
    <hr/>
判斷集合是否爲空:
    <s:property value="#request.list.isEmpty()"/>
    <hr/>
集合中的第一個元素:
    <s:property value="#request.list[0]"/>
    <hr/>
遍歷集合中的元素:<br/>
    <s:iterator value="#request.list" var="user">
        用戶名:<s:property value="#user.username"/><br/>
    </s:iterator>
    <hr/>
map集合大小:
    <s:property value="#request.map.size()"/>
    <hr/>
map集合keys:
    <s:property value="#request.map.keys"/>
    <hr/>
map集合values:
    <s:property value="#request.map.values"/>
    <hr/>
遍歷map集合元素:
    <br/>
    <s:iterator value="#request.map.values" var="keys">
        <s:property value="#keys.username"/>
        <br/>
    </s:iterator>
</body>
  • 輸出結果

這裏寫圖片描述

這裏寫圖片描述

發佈了50 篇原創文章 · 獲贊 305 · 訪問量 83萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章