(4)ResultType

<package name="ResultType" namespace="/r" extends="struts-default">
     <action name="login" class="com.action.UserAction" >
         <result>/login.jsp</result>
     </action>
     <!-- result元素定义中,可以使用Action的execute方法运行之后的数据 -->
     <action name="welcome" class="com.action.UserAction">
         <result>/${folder}.jsp</result><!-- 在execute中,可以返回不同的字符串(必须是Action中private属性),使得指定jsp位置的字符串是活动,而不是固定的。 
                                               下一个页面可以取到上一个请求对象(Action)里面的值-->
     </action>

      <action name="welcome1" class="com.action.UserAction">
         <result type="redirect">/${folder}.jsp?username=${username}</result><!-- 下一个页面不会取到上一个请求对象的值,如果要传值,可以采用get的方式传参
         该参数会存放在值栈的Stack Context的parameters中 -->
     </action>

     <action name="welcome2" class="com.action.UserAction">
         <result name="toSecond" type="chain">secondAction</result><!-- 在实际开发中,比如servlet开发中,一个请求被servlet处理后,不是直接产生响应,而是将
                      这个请求传递给下一个servlet中,多个servlet处理完后,才会产生响应。类似struct中,一个请求被一个Action处理后,也不是立刻产生响应,要经多个Action
                      才会产生响应,这时就需要chain。所以chain的作用:用来将Action执行完后连接到另一个Action中继续执行,新的Action可以使用上一个Action中的ActionContext -->
     <!-- <param name="namespace">其他package的namespace</param>  --><!-- 也就是chain链接的action是可以跨包的 -->
     </action>
      <action name="secondAction" class="com.action.secondAction">
         <result>/welcome.jsp</result><!-- 最终在值栈中:Value Stack Contents中有两个Action -->
     </action>
     <!-- 注意:在上面的chain中,<result>secondAction<result>不能写传递参数,因为它对应的是action名称 -->

        <action name="r1" >
           <result type="dispatcher"><!-- 在struct2中,把result当做实现mvc模型中的视图技术,在struct2中,可以使用多种视图技术,如jsp、freemarker、velocity、jfreechart
                                          在jsp视图中,type=dispatcher用的最多。在这个ResultType的实现中,调用了Java.servlet.RequestDispatcher类中的forward方法,也就是对RequestDispatcher
                                           的一个在封装
                                           -->
               /r1.jsp
           </result>
       </action>
           <!-- 上面的地址栏写http://localhost:8080/struct_ResultType/r/r1,就能访问到了r2.jsp页面 -->
           <!-- 
           dispatcher:服务器端跳转,客户端不知道已经调到另外一个页面,这个页面返回到客户端
           redirect:客户端发一个请求,然后在服务端redirect,相当于将此地址返回客户端,客户端发一个请求到redirect地址

            -->
           <action name="r2">
               <result type="redirect"><!--  在这个ResultType的实现中,它包装的是Http.ServletResponse类的sendRedirect方法-->
                 的一个在封装
                   /r2.jsp
               </result>
            </action>   
           <!-- 上面的地址栏写http://localhost:8080/struct_ResultType/r/r2 因为是redirect
             最终地址栏显示的是http://localhost:8080/struct_ResultType/r2.jsp

            -->

           <action name="r3">
               <result type="chain"><!-- 用于把相关的几个action连接起来,完成共同的功能 -->
                   r1<!-- 不要写/ -->
               </result>
            </action>     
            <!-- 上面地址栏显示的是http://localhost:8080/struct_ResultType/r/r3【请求也是这个】 -->

            <action name="r4">
               <result type="redirectAction">
                   r2
               </result>
            </action>     
              <!-- 上面地址栏显示的是http://localhost:8080/struct_ResultType/r2.jsp
              写的地址:localhost:8080/struct_ResultType/r/r4
               -->
    </package>
posted @ 2017-11-06 19:12  测试开发分享站  阅读(95)  评论(0编辑  收藏  举报