设为首页 加入收藏

TOP

Struts2 - View页面中获取Action的成员变量
2014-11-23 22:55:09 来源: 作者: 【 】 浏览:7
Tags:Struts2 View 页面 获取 Action 成员 变量

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


方法二: HttpRequest的getAttribute方法
实际上, 在Action处理完之后,跳到result页面之前,struts应用把Action对象的成员变量放到了request的属性里(机制未知)
所以,可以用request对象来获取Action的成员变量。
【似乎只能获取Object,不能获取int这种变量】

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


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇VC , Linux 静态编译与动态编译 (.. 下一篇Android实现对话框-附一个完整例子

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: