<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>
|
< result type = "redirect" name = "redirect"> / resulttype/resulttype.jsp </result > |
< include file = "struts-resulttype.xml" ></include > |
<?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>
<package name="resulttype" namespace="/" extends="struts-default" >
<!--
全局结果集:当某个方法返回为name属性对应的值时,就调用全局结果集进行处理。
可以用来进行通用错误页面的处理。
-->
<global-results>
<result name="error" >errot/error.jsp</ result>
</global-results>
<!-- 测试请求转发
-->
<action name= "dispatcherAction" method="testDispatcher" class="cn.itheima03.struts2.resulttype.ResultTypeAction" >
<!--
<result type="" name=""></result> :
result标签代表一种结果集,在struts-default.xml中定义了一写结果集
type
dispatcher 转发 默认值
redirect 重定向
redirectAction 重定向到一个action
name
success 默认值
method="testDispatcher"
-->
<result type= "redirect" name="redirect" >/ resulttype/resulttype.jsp</result >
</action>
<!-- 测试重定向
浏览器中的 url会发生变化:
http://localhost/itheima03_struts2/resulttype/resulttype.jsp
-->
<action name= "redirectAction" method="testRedirect" class="cn.itheima03.struts2.resulttype.ResultTypeAction" >
<result type= "redirect" name="redirect" >/ resulttype/resulttype.jsp</result >
</action>
<!--
测试重定向到一个action
浏览器的地址栏会变成:
http://localhost/itheima03_struts2/dispatcherAction!testDispatcher.action
-->
<action name= "redirectActionAction" method="testRedirectAction" class="cn.itheima03.struts2.resulttype.ResultTypeAction" >
<result type= "redirectAction" name="redirectAction" >dispatcherAction!testDispatcher.action </result>
</action>
<!-- 测试全局结果集的处理
当自己定义的name属性的值和全局结果集name属性值一直时,自己定义的优先!
1.当自己的name=‘error‘时,使用自己定义的结果集;
2.当自己的name!=‘error‘时,如果访问的方法返回的是"error",那么调用全局结果集进行处理!
-->
<action name= "globalResultAction_*" method ="{1}" class="cn.itheima03.struts2.resulttype.ResultTypeAction" >
<result name="error" >resulttype/resulttype.jsp</ result>
</action>
</package >
</struts>
|
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ResultTypeAction extends ActionSupport{
/**
* 测试请求转发结果集:type为dispatcher
* <result type="dispatcher" name="dispatcher">/resulttype/resulttype.jsp </result>
*/
public String
testDispatcher(){
ServletActionContext. getRequest().setAttribute("aa", "aadda");
// return "error";//测试全局结果集
return "dispatcher" ;
}
/**
* 测试重定向
*/
public String
testRedirect(){
ServletActionContext. getRequest().setAttribute("aa", "aaa");
return "redirect" ;
}
/**
* 测试重定向到一个action
* <result type="redirectAction" name="redirectAction">dispatcherAction!testDispatcher.action</result>
*/
public String
testRedirectAction(){
ServletActionContext. getRequest().setAttribute("aa", "aaa");
return "redirectAction" ;
}
/**
* 测试全局结果集
* 返回值要和全局结果集的name属性值要对应!
* <global-results>
<result name="error">errot/error.jsp </result>
</global-results>
*
* @return
*/
public String
globle(){
return "error" ;
}
}
|
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/damogu_arthur/article/details/46907151