歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2 - View頁面中獲取Action的成員變量

Struts2 - View頁面中獲取Action的成員變量

日期:2017/3/1 9:43:08   编辑:Linux編程

View頁面中獲取Action的成員變量

按照Struts的設計,在Action處理完後,把結果數據保存在自己的成員變量裡,然後跳至result指定的頁面(VIEW頁面)。
VIEW頁面負責展現處理結果,那VIEW中如何獲取Action的數據呢?

方法一:Struts2 Property Tag / OGNL
http://struts.apache.org/2.x/docs/using-struts-2-tags.html
http://www.vaannila.com/struts-2/struts-2-example/struts-2-ognl-expression-language-example-1.html

比如,在Action裡有一個成員變量helloCount及其對應的getter
private int helloCount = 0;

public int getHelloCount() {
return helloCount;
}

public void setHelloCount(int helloCount) {
HelloWorldAction.helloCount = helloCount;
}

則在VIEW.jsp中,
宣布使用struts的tag
<%@ taglib prefix="s" uri="/struts-tags" %>
可以使用下面的tag來顯示helloCount的值:
I've said hello <s:property value="helloCount" />

方法二: HttpRequest的getAttribute方法
實際上, 在Action處理完之後,跳到result頁面之前,struts應用把Action對象的成員變量放到了request的屬性裡(機制未知)
所以,可以用request對象來獲取Action的成員變量。
【似乎只能獲取Object,不能獲取int這種變量】

<%
String username = (String) request.getAttribute("username");
%>、

struts2文件上傳(保存為BLOB格式) http://www.linuxidc.com/Linux/2014-06/102905.htm

Struts2的入門實例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2實現ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm

遇到的Struts2文件下載亂碼問題 http://www.linuxidc.com/Linux/2014-03/98990.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

Struts2 注解模式的幾個知識點 http://www.linuxidc.com/Linux/2013-06/85830.htm

Struts 的詳細介紹:請點這裡
Struts 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved