- Result标签
- 作用 当action执行完毕,后要返回什么样的视图.
- Type属性 决定返回的是什么视图.
- struts-default.xml的Type属性的定义
-
1 <result-types> 2 <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> 3 <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> 4 <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> 5 <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> 6 <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> 7 <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> 8 <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> 9 <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> 10 <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> 11 <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> 12 <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" /> 13 </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配置
-
1 <action name="*_vote" class="com.vot.action.VoteAction" method="{1}"> 2 <result name="findVoteByChannelID" type="dispatcher">findVote.jsp</result> 3 <!--type 来自struts-default.xml 里面的配置的name属性.-->
4</action> - 全局Result配置
-
1 <package name="helloworld" extends="struts-default"> 2 <global-results> 3 <result name="toLogin">/login.jsp</result> 4 </global-results> 5 6 <action ……> 7 …… 8 </action> 9 </package>
-
- Result搜索的顺序
-
1 在有了全局Result之后,需要讨论一下在Action运行之后,根据execute方法的返回值寻找Result顺序了。 2 3 (1)首先,先找自己的<action>元素内的<result>元素是否有匹配的,如果有就执行这个Result,如果没有,下一步。 4 5 (2)其次,再找自己的<action>所在的包的全局Result,看看是否有匹配的,如果有就执行这个Result,如果没有,下一步。 6 7 (3)再次,递归的寻找自己的包的父包、祖父包中的全局Result是否有匹配的,如果有就执行这个Result,如果没有,下一步。 8 9 (4)最后,如果上述三种情况都没有匹配的Result的话,则抛出Exception。 10 11 注意:如果出现同名的Result,上述的顺序也是Result之间的优先顺序。也就是说,如果Action的execute方法返回的字符串,在局部Result和全局Result中都有能匹配的配置,那么以局部Result为准。
-
转载于:https://www.cnblogs.com/laxilo/p/7243047.html