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

简介:
重定向需要传值的原因就是栈值不共享

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
相关文章
|
11月前
|
网络协议 Java 数据库连接
Springboot 通用返回类Result
Springboot 通用返回类Result
332 0
|
应用服务中间件 Apache nginx
Yii框架中的'enablePrettyUrl' => true, 这段代码是干什么的?底层原理是什么?为什么这样写?
Yii框架中的'enablePrettyUrl' => true, 这段代码是干什么的?底层原理是什么?为什么这样写?
119 0
SpringMVC学习(三):路径中的RESTful风格占位符
SpringMVC学习(三):路径中的RESTful风格占位符
174 0
SpringMVC学习(三):路径中的RESTful风格占位符
项目中自定义通用返回值Result
项目中自定义通用返回值Result
145 0
|
缓存 监控 Dubbo
谈谈Java接口Result设计
谈谈Java接口Result设计
谈谈Java接口Result设计
|
XML 数据格式
resultMap自定义某个javaBean的封装规则代码
select * from emp where id=#{id}   
667 0