Struts2_带参数的结果集

页面请求:

<a href="user/user?type=1">传参数</a>

action:

 1     public Integer type;
 2     
 3     public String execute(){
 4         return SUCCESS;
 5 //        return "test";
 6     }
 7 
 8     public Integer getType() {
 9         return type;
10     }
11 
12     public void setType(Integer type) {
13         this.type = type;
14     }

stuts.xml中把type这个参数传到其他的jsp页面:

1 <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
2             <!-- $用来在配置文件里面往值栈 valueStack 里取值 -->
3             <result type="redirect">/user_success.jsp?t=${type}</result>
4             <result name="test" type="chain">test</result>
5         </action>

注意:一次request共享一个值栈,要想跨request,比如重定向跳转了,那么就可以通过这种方式传递参数。

jsp页面:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="/struts-tags" prefix="s"%>
 4 <%
 5 String path = request.getContextPath();
 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 7 %>
 8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 9 <html>
10 <head>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>user_success</title>
13 <base href="<%=basePath%>"/>
14 </head>
15 <body>
16 <h1>user success.</h1>
17 值栈中没值,因为是通过参数传过来的,通过redirect的不在同一个request中<br>
18 from valuestack:<s:property value="t"/><br>
19 所以只能通过actionContext来取,具体是parameters
20 from actioncontext:<s:property value="#parameters.t"/>
21 <s:debug></s:debug>
22 </body>
23 </html>

结果:

链接: http://pan.baidu.com/s/1mic8CZm 密码: q5mh

原文地址:https://www.cnblogs.com/ShawnYang/p/6674737.html