struts2中的result介绍
result在struts.xml中的type配置和说明
这里的r1.jsp,r2.jsp和a.jsp内容随便写,用来区别显示效果就行
<package name="resultpack" extends="struts-default" namespace="/r">
<!-- 全局result,这个package里的所有action都可以访问 -->
<global-results>
<result name="a">/a.jsp</result>
</global-results>
<!-- 服务器跳转,只能跳转到页面 ,默认是此项配置 客户端发起请求,访问某个url,在服务器条到另一个界面,客户端不知道 -->
<action name="r1">
<result type="dispatcher">
/r1.jsp
</result>
</action>
<!-- 客户端跳转,只能跳转到页面 客户端发起请求,访问某个url,服务器返回redirect反馈,然后浏览器发起一个新的请求 -->
<action name="r2">
<result type="redirect">
/r2.jsp
</result>
</action>
<!-- forward方式,访问的是action,可以跨包访问action -->
<action name="r3">
<result type="chain">
r1
</result>
</action>
<!-- forward方式跨包访问action -->
<action name="r31">
<result type="chain">
<param name="actionName">r5</param>
<param name="namespace">/rr</param>
</result>
</action>
<!-- 跳转到另一个action -->
<action name="r4">
<result type="redirectAction">
r2
</result>
</action>
</package>
<!-- 这里的extends中写的是上面包的名字,这样就可以访问resultpack中的全局result -->
<package name="result1" extends="resultpack" namespace="/rr" >
<action name="r5">
<result type="dispatcher">
/r1.jsp
</result>
</action>
</package>
动态结果集和参数的传递
在struts.xml中配置动态结果集和参数传递
<package name="result" extends="struts-default" namespace="/rr" >
<!-- 动态结果集 -->
<!-- 从值栈(valuestack)中取值 -->
<action name="user" class="com.wz.action.UserAction">
<result>
${r}
</result>
</action>
<!-- 给Result传递参数 -->
<action name="resparam" class="com.wz.action.ResultParamAction">
<result type="redirect">
/resparam_success.jsp?t=${type}
</result>
</action>
</package>
配置传递参数用到的action
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
* 一次request只有一个值栈
* forward过程中的几个action共享一个值栈,因为他们在同一次请求(request)中
* 但是客户端跳转(redirect)就不能共享,也就无法直接获取值,所以需要传递参数
*/
public class ResultParamAction extends ActionSupport {
private int type;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
配置动态结果集用到的action
public class UserAction extends ActionSupport{
private int type;
private String r;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getR() {
return r;
}
public void setR(String r) {
this.r = r;
}
//这里的r就是返回的结果
@Override
public String execute() throws Exception {
if(type==1){
r="/r1.jsp";
}else if(type==2){
r="/r2.jsp";
}
return "success";
}
}
显示传递参数的效果的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 从值栈中取值是没有值的,因为redirect访问的不是action
没有将值放在值栈中,所以下面这一行不会显示t的值 -->
<s:property value="t"/><br>
<!-- 在parameters中取 -->
<s:property value="#parameters.t"/>
<s:debug></s:debug>
</body>
</html>