
- Result标签
- 作用 当action执行完毕,后要返回什么样的视图.
- Type属性 决定返回的是什么视图.
- struts-default.xml的Type属性的定义
-
<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-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
</result-types>Struts2框架提供的结果类型
已配置结果类型名 类 名 描 述 dispatcher org.apache.struts2.dispatcher.
ServletDispatcherResult默认结果类型,用来呈现JSP页面 chain com.opensymphony.xwork2.
ActionChainResult将action和另外一个action链接起来 freemarker org.apache.struts2.views.freemarker.
FreemarkerResult呈现Freemarker模板 httpheader org.apache.struts2.dispatcher.
HttpHeaderResult返回一个已配置好的HTTP头信息响应 redirect org.apache.struts2.dispatcher.
ServletRedirectResult将用户重定向到一个已配置好的URL redirectAction org.apache.struts2.dispatcher.
ServletActionRedirectResult将用户重定向到一个已定义好的action stream org.apache.struts2.dispatcher.
StreamResult将原始数据作为流传递回浏览器端,
该结果类型对下载的内容和图片非常有用velocity org.apache.struts2.dispatcher.
VelocityResult呈现Velocity模板 xslt org.apache.struts2.views.xslt.
XSLTResult呈现XML到浏览器,
该XML可以通过XSL模板进行转换plaintext org.apache.struts2.dispatcher.
PlainTextResult返回普通文本类容
-
- Result配置
<action name="*_vote" class="com.vot.action.VoteAction" method="{1}">
<result name="findVoteByChannelID" type="dispatcher">findVote.jsp</result>
3 <!--type 来自struts-default.xml 里面的配置的name属性.-->
4</action>- 全局Result配置
<package name="helloworld" extends="struts-default">
<global-results>
<result name="toLogin">/login.jsp</result>
</global-results> <action ……>
……
</action>
</package>
- Result搜索的顺序
在有了全局Result之后,需要讨论一下在Action运行之后,根据execute方法的返回值寻找Result顺序了。 (1)首先,先找自己的<action>元素内的<result>元素是否有匹配的,如果有就执行这个Result,如果没有,下一步。 (2)其次,再找自己的<action>所在的包的全局Result,看看是否有匹配的,如果有就执行这个Result,如果没有,下一步。 (3)再次,递归的寻找自己的包的父包、祖父包中的全局Result是否有匹配的,如果有就执行这个Result,如果没有,下一步。 (4)最后,如果上述三种情况都没有匹配的Result的话,则抛出Exception。 注意:如果出现同名的Result,上述的顺序也是Result之间的优先顺序。也就是说,如果Action的execute方法返回的字符串,在局部Result和全局Result中都有能匹配的配置,那么以局部Result为准。