概述

<action name="helloworld" class="com.liuyong666.action.HelloWorldAction">    <result name="success">/WEB-INF/page/hello.jsp</result></action>
  • result配置相似于struts1中的forward,但struts2中提供了多种后果类型,罕用的类型有:

    • dispatcher(默认值)
    • redirect(重定向)
    • redirectAction
    • plainText
  • 通过type属性值指定。

后果类型

  • redirect

    • 在result中还能够应用${属性名}表达式拜访action中的属性,表达式里的属性名对应action中的属性。如下:
    • <result type="redirect">/view.jsp?id=${id}</result>
  • redirectAction

    • 如果重定向的action在同一个包下:
    • <result type="redirectAction">helloworld</result>
    • 如果重定向的action在别的命名空间下:
    • <result type="redirectAction">        <param name="actionName">helloworld</param>        <param name="namespace">/test</param></result>
  • plainText

    • 显示原始文件内容,例如:当咱们须要原样显示jsp文件源代码的时候,咱们能够应用此类型。
    • <result name="source" type="plainText">        <param name="location">/xxx.jsp</param>        <param name="charSet">UTF-8</param><!-- 指定读取文件的编码 --></result>

全局后果集(Globle Result)

  • 当有多个Action应用同一个后果集时,则能够应用全局后果集(Globle Result),如下:
  • <global-results> <!-- 定义在包里 -->         <result name="mainpage">/main.jsp</result></global-results>
  • 多个包想应用同一个后果集,能够利用包的继承,实现代码重用。

    • 创立一个名叫"base"的package,让它继承"struts-default"
    • 该package里放入共用的global-results
    • 想要应用该后果集的包继承"base"