您的位置:首页 > 编程语言 > Java开发

【Struts2框架】第二节Result-向结果传参数

2015-07-23 20:26 411 查看
重定向需要传值的原因就是栈值不共享

struts.xml:
<package name="resultTypes" namespace="/r" extends="struts-default">
<action name="result2" class="cn.edu.hpu.action.ResultAction2">
<result type="redirect">/result3.jsp?t=${type}</result>
<action/>
</package>


ResultAction2.java:
package cn.edu.hpu.action;

import com.opensymphony.xwork2.ActionSupport;

public class ResultAction2 extends ActionSupport {

private int type;

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

@Override
public String execute() throws Exception {
return SUCCESS;
}

}
链接:
<a href="<%=basePath %>r/result2?type=110">跳转传参</a>


结果页面result3.jsp:
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'result3.jsp' starting page</title>
</head>

<body>
User SUCCESS!! <br/>
<!-- s:property从值栈中取值,但是由于重定向不共享值栈,所以用这种方法取不出值 -->
<s:property value="t"/>
<!-- 用下面这种方法可以取值(从parameters中取值) -->
<s:property value="#parameters.t"/>
<s:debug></s:debug>
</body>
</html>


*注:关于值栈

一次request只有一个值栈,服务器端的forword()对于客户端来说就是一个request,所以两个东西共享一个值栈。

需要传参的情况是这样的,请求的时候不是在服务端跳转,而是在客户端跳转,即是发起一个新请求,这个请求转发到另一个request上,这是两个request,这时候就不会共享两个值栈。

说白了就是转发(forward())和重定向(redirect())的时候,转发可以共享值栈,但是重定向不可以,所以跳转方式为重定向的时候,需要传参数。

转载请注明出处:http://blog.csdn.net/acmman/article/details/47027753
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: