您的位置:首页 > 编程语言 > Java开发

【struts2】Result和ResultType

2015-11-25 11:53 525 查看
 简单的说,Result是Action执行完后返回的一个字符串,它指示了Action执行完成后,下一个页面在哪里。Result仅仅是个字符串,仅仅是用来指示下一个页面的,那么如何才能够到达下一个页面呢?下一个页面如何能正确地展示结果呢?这就该引出一个新概念——ResultType,所谓ResultType,指的是具体执行Result的类,由它来决定采用哪一种视图技术,将执行结果展现给用户。

  很多时候,我们并不去区分Result和ResultType,而是笼统的称为Result。因此,Result除了当作上面讲述的字符串来理解外,还可以把Result当作技术,把Result当作实现MVC模型中的View视图的技术,也就是ResultType来看待。

  在Struts2中,可以使用多种视图技术,如jsp、freemarker、velocity、jfreechart等等,同时,Struts2也支持用户自定义ResultType,来打造自己的视图技术。

  1)预定义ResultType

  在Struts2中,预定义了很多ResultType,其实就是定义了很多展示结果的技术。Struts2把内置的<result-type>都放在struts-default包中。struts-default包就是我们配置的包的父包,这个包定义在struts-2.3.16.3.jar包中的根目录下的文件struts-default.xml中。在这个包中,可以找到相关的<result-type>的定义,<result-types>元素是<package>元素的直接子元素。Struts2预定义如下:



<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>




  上面的每一个<result-type>元素都是一种视图技术或跳转方式的封装。其中的name属性是在<result>元素中如何引用这种视图技术或跳转方式,对应着<result>元素的type属性的值。

  可能有朋友会说,我们在配置<result>元素的时候,没有配置过type属性嘛。没错,你确实没有配置过,原因就在于Struts2里面设置了默认的type,如果你没有配置,默认就是“dispatcher”。这个“dispatcher”的技术就相当于在Servlet里面的“RequestDispatcher”的技术,也就是一个页面跳转的技术。而class属性是这种视图技术或跳转方式的具体实现类,这些实现类都已经是Struts2实现好的,我们只需要引用就可以了。

  2)Result的配置

配置name属性

  name属性是用来跟Action的execute方法返回的字符串相对应的,用来指示Action运行后跳转到的下一个页面,因此name属性的值可以是任意字符串。比如有如下的execute方法:

public String execute() throws Exception {
return "toWelcome";
}


  那么,这里返回的“toWelcome”,在struts.xml里面就有如下的配置来对应:

<action name="helloworldAction" class="cn.javass.action.action.HelloWorldAction">
<result name="toWelcome">/s2impl/welcome.jsp</result>
</action>


  如果不设置的话,默认值为“success”,正好和Action中的“SUCCESS”这个常量相对应,那样的话,execute方法就应该返回SUCCESS,如下:

public String execute() throws Exception {
return this.SUCCESS;
}


  此时在struts.xml里面就有如下的配置来对应:

<action name="helloworldAction" class="cn.javass.action.action.HelloWorldAction">
<result>/s2impl/welcome.jsp</result>
<!-- 或者如下的配置
<result name="success">/s2impl/welcome.jsp</result>
-->
</action>


配置type属性

  <result>元素的type属性也可以是任意字符串,不过,一定是某一个<result-type>元素的name属性。在没有自定义ResultType的情况下,type属性的值,就是在struts-default.xml中所定义的<result-type>的name属性的值。比如:

<action name="helloworldAction" class="cn.javass.action.action.HelloWorldAction">
<result name="toWelcome" type="dispatcher">/s2impl/welcome.jsp</result>
</action>


  这里的“dispatcher”就是在struts-default.xml中所定义的默认的<result-type>的name属性值。既然是默认的,那就可以不用配置,也就是说,上面的配置跟如下配置是等价的,示例如下:

<action name="helloworldAction" class="cn.javass.action.action.HelloWorldAction">
<result name="toWelcome">/s2impl/welcome.jsp</result>
</action>


  3)全局Result

配置方法

  全局Result本身没有任何的特异之处,同样是配置name属性和type属性,包括如何指定jsp的位置都和普通的Result一样,只不过其<result>元素并不是<action>元素的子元素,而是作为<global-results>元素的子元素,而<global-results>元素又是<package>元素的子元素,示例如下:



<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为准。

  参考资料:http://www.iteye.com/topic/1124526
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: