Struts2中result类型说明

       Result的首要职责,是封装Action层到View层的跳转逻辑。在Action执行完毕之后,框架需要把代码的执行权重新交还给Web容器,并转向到相应的页面或者其他类型的

View层。而这个跳转逻辑,就由Result来完成。

  View层的显示类型非常多,有最常见的JSP、当下非常流行的Freemarker/Velocity模板、Redirect到一个新的地址、文本流、图片流、甚至是JSON对象等等。所以Result

层的独立存在,就能够对这些显示类型进行区分,并封装合理的跳转逻辑。

首先,我们打开Struts2-core-2.3.16.jar/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-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
        </result-types>

dispatcher:

         dispatcher主要用于返回JSP,HTML等以页面为基础View视图,这个也是Struts2默认的Result类型。在使用dispatcher时,唯一需要指定的,是JSP或者HTML页面的位置,这个位置将被用于定位返回的页面;

<result name="success">/index.jsp</result> 

freemarker / velocity

             随着模板技术的越来越流行,使用Freemarker或者Velocity模板进行View层展示的开发者越来越多。Struts2同样为模板作为Result做出了支持。由于模板的显示需要模板(Template)与数据(Model)的紧密配合,所以在Struts2中,这两个Result的主要工作是为模板准备数据。


redirect

redirect是重定向到一个URL,如果你需要对目标地址传递参数,那么需要在目标地址url或者配置文件中指出:

  <action name="index2">
        <result type="redirect">/index2.jsp</result>
  </action>
访问时:http://localhost:8080/Struts2_02/index2,跳转后的页面是:http://localhost:8080/Struts2_02/index2.jsp


redirectAction

<action name="financeReport" class="com.mycompany.reports.FinanceReportAction"> 
	<result name="input">/jsp/index.jsp</result>            
	<result name="success" type="redirectAction">
		<param name="actionName">displayReport</param>
		<param name="parse">false</param>
		<param name="anchor">SUMMARY</param>
	</result>
</action>


chain

<package name="public" extends="struts-default">
    <!-- Chain creatAccount to login, using the default parameter -->
    <action name="createAccount" class="...">
        <result type="chain">login</result>
    </action>

    <action name="login" class="...">
        <!-- Chain to another namespace -->
        <result type="chain">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>
    </action>
</package>

<package name="secure" extends="struts-default" namespace="/secure">
    <action name="dashboard" class="...">
        <result>dashboard.jsp</result>
    </action>
</package>

struts2中关于result的返回类型一般我们是转发到一个jsp页面或者是html页面等,但是struts2中的result的返回类型还有redirect,redirectAction,chain

当使用type=“redirectAction” 或type=“redirect”提交到一个action并且需要传递一个参数时。这里是有区别的:

一、使用redirect需要后缀名 使用redirectAction不需要后缀名
二、type="redirect" 的值可以转到其它命名空间下的action,而redirectAction只能转到同一命名空下的 action,因此它可以省略.action的后缀直接写action的名称。

如:

<result name="success" type="redirect">viewTask.action</result>
<result name="success" type="redirectAction">viewTask</result>

(1)redirect:action处理完后重定向到一个视图资源(如:jsp页面),请求参数全部丢失,action处理结果也全部丢失。

(2)redirectAction:action处理完后重定向到一个action,请求参数全部丢失,action处理结果也全部丢失。

(3)chain:action处理完后转发到一个action,请求参数全部丢失,action处理结果不会丢失。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章