现在的位置: 首页 > 综合 > 正文

的各种转发类型

2018年01月18日 ⁄ 综合 ⁄ 共 1244字 ⁄ 字号 评论关闭

<result>标签中的type属性决定如何处理客户端的请求:

result配置类似于struts1中的forward,但struts2中提供了多种结果类型,常用的类型有: dispatcher(默认值)、 redirect 、 redirectAction 、 plainText。

      (1)dispatcher  转发 (地址栏不会变)

      (2)redirect    重定向,重定向的url不能是WEB-INF目录下的资源 (地址栏会变)

      (3)redirectAction 重定向到本package或其他package的action处理。(地址栏会变)

      (4)plainText  将资源文件源文件输出到客户端(地址栏不会变)


(1)dispatcher
(缺省值)

<action name="helloworld" class="cn.itcast.action.HelloWorldAction">

    <result name="success">/WEB-INF/page/hello.jsp</result>
</action>

(2)redirect   
在result中还可以使用${属性名}表达式访问action中的属性,表达式里的属性名对应action中的属性。如下:

<result type="redirect">/view.jsp?id=${id}</result>


(3)redirectAction
下面是redirectAction 结果类型的例子,如果重定向的action在同一个命名空间下:
<result type="redirectAction">helloworld</result>
如果重定向的action在别的命名空间下:
  <action name="regist">
       <result type="redirectAction">
           <param name="actionName">helloAction</param>
           <!-- 如果是同一个命名空间,则可以不要下面的参数 -->
           <param name="namespace">/test</param>
           <!-- 如果不配method参数 ,则method值是actionName参数对应的method-->
           <param name="method">execute</param>
       </result>
 </action>


(4)plaintext:显示原始文件内容,例如:当我们需要原样显示jsp文件源代码 的时候,我们可以使用此类型。
<result name="source" type="plainText ">
    <param name="location">/xxx.jsp</param>
    <param name="charSet">UTF-8</param><!-- 指定读取文件的编码 -->
</result>

抱歉!评论已关闭.