Struts2 Result Types 详解:结果类型与应用场景
在 Struts2 中,result元素用于定义 Action 执行后的跳转逻辑,而type属性则决定了跳转的方式(如转发、重定向等)。Struts2 内置了多种结果类型,适配不同的业务场景。本文基于struts-default.xml中的配置,详细介绍常用的 Result Type 及其使用方法。
Result Type 的核心概念
- 定义:Result Type 是 Struts2 中用于处理 Action 返回结果的组件,负责将请求转发或重定向到目标资源(页面、Action、文件等)。
- 配置位置:所有内置 Result Type 定义在
struts-default.xml的<result-types>节点中,通过继承struts-default包即可使用。 - 核心属性:
name:结果标识(如"success"、"error"),与 Action 方法返回的字符串匹配;type:结果类型(如dispatcher、redirect),决定跳转方式;location:目标资源路径(可省略,直接写在<result>标签体中)。
常用 Result Type 详解
1. dispatcher(默认类型)
类路径:
org.apache.struts2.result.ServletDispatcherResult功能:服务器内部转发(Forward)到 JSP 或 HTML 页面,是最常用的结果类型。
特点:
- 地址栏 URL 不变(显示原请求地址);
- 共享同一请求上下文(可通过
request传递数据); - 不能直接转发到 Action(需用
chain类型)。
配置示例:
1
2
3
4<action name="showInfo">
<!-- 转发到/pages/info.jsp(默认type="dispatcher") -->
<result>/pages/info.jsp</result>
</action>
2. redirect
类路径:
org.apache.struts2.result.ServletRedirectResult功能:重定向(Redirect)到页面或外部 URL,会发送新的 HTTP 请求。
特点:
- 地址栏 URL 变为目标资源地址;
- 不共享请求上下文(需通过 URL 参数传递数据);
- 可重定向到页面、外部 URL 或 Action(需指定完整路径)。
配置示例:
1
2
3
4
5
6
7
8
9<action name="logout">
<!-- 重定向到登录页面 -->
<result type="redirect">/login.jsp</result>
</action>
<action name="goBaidu">
<!-- 重定向到外部URL -->
<result type="redirect">https://www.baidu.com</result>
</action>
3. redirectAction
类路径:
org.apache.struts2.result.ServletActionRedirectResult功能:专门用于重定向到另一个 Action,自动处理命名空间(
namespace)。特点:
- 本质是重定向,地址栏 URL 会变化;
- 无需手动拼接 Action 路径,支持跨命名空间跳转。
配置示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14<!-- 当前包namespace="/user" -->
<action name="updateUser">
<!-- 重定向到同命名空间的listUser Action -->
<result type="redirectAction" name="success">listUser</result>
</action>
<action name="deleteUser">
<!-- 重定向到namespace="/admin"的manageAction -->
<result type="redirectAction" name="success">
<param name="actionName">manageAction</param>
<param name="namespace">/admin</param>
<param name="id">${userId}</param> <!-- 传递参数 -->
</result>
</action>
4. chain
类路径:
com.opensymphony.xwork2.ActionChainResult功能:转发到另一个 Action(同一次请求内),形成 Action 链。
特点:
- 地址栏 URL 不变(显示原请求地址);
- 共享请求上下文(可通过
ActionContext传递数据); - 适用于多步骤处理(如先验证再执行)。
配置示例:
1
2
3
4
5
6
7
8<action name="checkUser">
<!-- 转发到同命名空间的saveUser Action -->
<result type="chain" name="success">saveUser</result>
</action>
<action name="saveUser">
<result>/pages/success.jsp</result>
</action>
5. stream
类路径:
org.apache.struts2.result.StreamResult功能:以流的形式输出数据,常用于文件下载、动态图片生成等场景。
核心参数:
contentType:MIME 类型(如application/octet-stream表示二进制流);inputName:Action 中返回输入流的属性名(默认inputStream);contentDisposition:指定下载文件名(如attachment;filename="file.txt")。
配置示例:
1
2
3
4
5
6
7<action name="download" class="com.xxx.DownloadAction">
<result type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param> <!-- Action中的输入流属性 -->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
</result>
</action>对应的 Action 需提供输入流:
1
2
3
4
5
6
7
8
9
10
11
12public class DownloadAction {
private InputStream fileInputStream;
private String fileName;
public String execute() throws Exception {
fileInputStream = new FileInputStream(new File("D:/test.txt"));
fileName = "test.txt";
return "success";
}
// getter和setter
}
6. 其他常用类型
- freemarker:用于渲染 Freemarker 模板(
.ftl文件),需配置 Freemarker 依赖。 - velocity:用于渲染 Velocity 模板(
.vm文件)。 - httpheader:用于返回自定义 HTTP 响应头(如设置 403 禁止访问)。
- plainText:以纯文本形式输出结果(避免解析 HTML 标签)。
Result Type 的选择原则
| 场景需求 | 推荐类型 | 理由 |
|---|---|---|
| 跳转至 JSP 页面,需共享数据 | dispatcher |
转发可共享 request,地址栏不变。 |
| 跳转后不允许刷新重复提交 | redirect |
重定向会清除原请求,避免表单重复提交。 |
| 跳转至另一个 Action | redirectAction |
专门处理 Action 间重定向,自动处理命名空间。 |
| 多步骤 Action 处理(共享数据) | chain |
转发到 Action,共享上下文,适合流程化操作。 |
| 文件下载或二进制流输出 | stream |
以流的形式输出数据,支持大文件下载。 |
配置技巧与注意事项
路径写法:
- 以
/开头的路径是绝对路径(相对于 Web 应用根目录); - 不以
/开头的路径是相对路径(相对于当前 Action 的命名空间)。
- 以
参数传递:
- 重定向(
redirect/redirectAction)需通过 URL 参数传递数据(如?id=${id}); - 转发(
dispatcher/chain)可通过request或ActionContext传递数据。
- 重定向(
避免循环跳转:
- 使用
chain或redirectAction时,需确保 Action 间不形成循环(如 A→B→A),否则会导致栈溢出。
- 使用
自定义 Result Type:
若内置类型不满足需求,可实现Result接口自定义类型,并在struts.xml中注册:1
2
3<result-types>
<result-type name="myResult" class="com.xxx.MyResult"/>
</result-types>