目录
1 概述
在 Action 类中,每个 action 方法均返回一个 String 类型的值, Struts 将根据这个值来决定响应什么结果。
在配置文件中,每个 Action 元素可包含多个 result 元素, 每个 result 元素分别对应着 action 方法的一个返回值。
result 元素属性
name: result 名字, 默认值为 success,与 Action 方法返回值相匹配,否则可能出错。
type: result类型. 默认缺省值为 dispatcher。
result 常见类型(定义在 struts-default.xml)
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult"default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
2 常用三种 Result 类型
(1) dispatcher
dispatcher------类型 ServletDispatcherResult
它是 type 属性的一个默认值(缺省时默认),转发控制权。
(2) redirect
redirect----- 类型 ServletRedirectResult
响应重定向到另一个资源(包括外部资源), 不是转发。
redirect 参数:
location: 重定向目的地
param: 是否把 location 参数的值视为一个 OGNL 表达式来解释. 默认值为 true。
(3) redirectAction
redirectAction------类型 ServletActionRedirectResult
响应重定向到另一个 Action。
为 ServletDispatcherResult 子类,redirectAction 结果类型和 redirect 结果类型后台工作原理一样,即利用 HttpServletResponse 的 sendRedirect 方法将请求重定向到指定的 URL。
redirectAction 主要用于重定向到 action。如需重定向到另一个 action,建议使用 redirectAction。
3 全局 result
概念:多个 action 中可共同使用的 result。
<package>
<global-results>
<result name="globalSuccess">/globalSuccess.jsp</result>
</global-results>
</package>
局部与全局出现相同的 result 时,局部起作用(“强龙不压地头蛇”)。
4 示例
package com.tianci.struts2_result;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ResultAction extends ActionSupport
{
@Override
public String execute() throws Exception
{
ServletActionContext.getRequest().setAttribute("username",
" test result type ");
// dispatcher
// return "dispatcherSuccess";
// redirect
// return "redirectSuccess";
// redirectAction
return "redirectActionSuccess";
}
}
xml 子文件配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="fileuploadresource" />
<package name="struts2_result" namespace="/struts2_result"
extends="struts-default">
<!--全局result-->
<global-results>
<result name="dispatherSuccess"> /struts2_result/GlobalSuccess.jsp</result>
<!-- name的值当局部和全局重名的时候,局部起作用。 -->
</global-results>
<!--dispatcher类型-->
<action name="resultAction" class="com.tianci.struts2_result.ResultAction">
<result name="dispatcherSuccess" type="dispatcher">
/struts2_result/DispatcherSuccess.jsp
</result>
<!--redirect类型-->
<result name="redirectSuccess" type="redirect">
/struts2_result/RedirectSuccess.jsp
</result>
<!--redirectAction类型-->
<result name="redirectActionSuccess" type="redirectAction">
<param name="actionName">helloWorldAction?username=${#request.username}</param><!--另一个action的名字,这里是给这个action传递了一个参数值-->
<param name="namespace">/struts2_test_1</param> <!--它的命名空间值-->
</result>
</action>
</package>
</struts>

6万+




被折叠的 条评论
为什么被折叠?



