struts-default.xml中默认result定义,如下:
<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接口源码,如下:
package com.opensymphony.xwork2;
import java.io.Serializable;
public interface Result extends Serializable {
publicvoid execute(ActionInvocation invocation) throws Exception;
}
返回类型说明(不管是什么类型,最终就是调用HttpResponse使用HTTP协议输出一堆字符串;)
chain
服务器端跳转,action跳action;
dispatcher
服务器端跳转,action跳jsp;默认是dispatcher返回;
freemarker
服务器端跳转,action跳.ftl模板;
httpheader
客户端跳转,action返回http协议的特定属性到客户端;
redirect
客户端重定向,action跳客户端再跳服务器的url;浏览器重定向不能重定向至WEB-INF目录,WEB-INF只能是服务器端转发JSP给客户端浏览器;
redirectAction
客户端重定向,action跳客户端再跳服务器的action;
stream
客户端跳转,action返回一个数据流到客户端浏览器;
velocity
服务器端跳转,action跳.vm模板;
xslt
服务器端跳转,action跳xml/xslt模板;
plainText
直接返回原文件内容,例如文件源码;默认是使用系统默认编码来读取对应的文件的,可以在XML中通过param来配置location、charSet;具体可以去看实现类的帮助文档或者源码;
注意:
1)struts.xml中,一般都要继承extends="struts-default"包,这样才能使用默认包中的定义,子类可以访问父类的定义;
2)struts.xml中,当一个包声明为abstract="true"时,这个包只能用来做声明,不能定义Action;
3)Action与Result都可以在XML配置中,加入param参数标签,为Action注入参数值;在重定向Action的时候,如果不在一个包中,可以通过设置param来指定具体的Action路径来访问;
4)在.properties和.xml配置文件中,可以通过OGNL表达式(${XXX})来访问ValueStack里面的值;
5)可以在struts.xml中定义全局Result跟exception跳转