struts2 - View页面中获取Action的成员变量

来源:互联网 发布:jpeg标准量化算法 编辑:程序博客网 时间:2024/04/28 12:48


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");
%>

0 0
原创粉丝点击