struts2.1.6 result 04

Result

1. 常用四种类型:

a) dispatcher(默认)

b) redirect

c) chain

d) redirectAction

2. 全局结果集

a) global-results | extends

3. 动态结果(了解)

a) 在action中保存一个属性,存储具体的结果location

4. 传递参数

a) 客户端跳转才需要传递

b) ${}表达式(不是EL)



result 类型 跳转

  1. Result 类型   
  2. dispatcher   
  3. redirect   
  4. chain   
  5. redirectAction   
  6. freemarker   
  7. httpheader   
  8. stream   
  9. velocity   
  10. xslt   
  11. plaintext   
  12. tiles   

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <html>  
  8.   <head>  
  9.   </head>  
  10.   <body>  
  11.     Result 类型  
  12.     <ol>  
  13.         <li><a href="r/r1">dispatcher</a></li>  
  14.     <li><a href="r/r2">redirect</a></li>  
  15.     <li><a href="r/r3">chain</a></li>  
  16.     <li><a href="r/r4">redirectAction</a></li>  
  17.     <li>freemarker</li>  
  18.     <li>httpheader</li>  
  19.     <li>stream</li>  
  20.     <li>velocity</li>  
  21.     <li>xslt</li>  
  22.     <li>plaintext</li>  
  23.     <li>tiles</li>  
  24.     </ol>  
  25.   </body>  
  26. </html>  
  27.   
  28.  <package name="resultType" extends="struts-default" namespace="/r">  
  29.         <action name="r1">  
  30.             <result type="dispatcher">   
  31.                 /r1.jsp  
  32.             </result>  
  33.         </action>  
  34.           
  35.          <action name="r2">  
  36.             <result type="redirect">   
  37.                 /r2.jsp  
  38.             </result>  
  39.         </action>  
  40.           
  41.          <action name="r3">  
  42.             <result type="chain">   
  43.                 r1  
  44.             </result>  
  45.         </action>  
  46.           
  47.          <action name="r4">  
  48.             <result type="redirectAction">   
  49.                 r2  
  50.             </result>  
  51.         </action>  
  52.     </package>   


GlobalResult

  1. package com.demo.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class AdminAction extends ActionSupport {  
  6.     @Override  
  7.     public String execute() throws Exception {  
  8.         return "mainpage" ;  
  9.     }  
  10. }  
  11.   
  12. package com.demo.action;  
  13.   
  14. import com.opensymphony.xwork2.ActionSupport;  
  15.   
  16. public class UserAction extends ActionSupport{  
  17.     private int type;  
  18.   
  19.     public int getType() {  
  20.         return type;  
  21.     }  
  22.   
  23.     public void setType(int type) {  
  24.         this.type = type;  
  25.     }  
  26.       
  27.     @Override  
  28.     public String execute() throws Exception {  
  29.         if(type == 1) {  
  30.             return   
  31.             "success" ;  
  32.         } else if (type == 2) {  
  33.             return "error" ;  
  34.         }  
  35.         return "mainpage" ;  
  36.     }  
  37. }  
  38.   
  39.    
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <html>  
  8.   <head>  
  9.   </head>  
  10.   <body>  
  11.     Resutl 类型  
  12. <ol>  
  13.     <li><a href="user/user?type=1">返回success</a></li>  
  14.     <li><a href="user/user?type=2">返回error</a></li>  
  15.     <li><a href="user/user?type=3">返回global result</a></li>  
  16.     <li><a href="admin/admin">admin,继承user包</a></li>  
  17. </ol>  
  18. </html>  

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <constant name="struts.devMode" value="true"/>       
  8.     <package name="user" extends="struts-default" namespace="/user">  
  9.     
  10.      <global-results>  
  11.         <result name="mainpage">/main.jsp</result>  
  12.      </global-results>  
  13.           
  14.       
  15.             
  16.       <action name="user" class="com.demo.action.UserAction">  
  17.          <result>/user_success.jsp</result>   
  18.         <result name="error">/user_error.jsp</result>  
  19.       </action>  
  20.     </package>  
  21.       
  22.     <package name="admin" namespace="/admin" extends="user">  
  23.         <action name="admin" class="com.demo.action.AdminAction">  
  24.             <result>/admin.jsp</result>  
  25.         </action>  
  26.     </package>  
  27. </struts>  

 <package name="admin" namespace="/admin" extends="user">  继承上面package包的内容

共用的actioon

  1. <global-results>  
  2.     <result name="mainpage">/main.jsp</result>  
  3. </global-results>  

DynamicResult

  1. package com.demo.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class UserAction extends ActionSupport {  
  6.     private int type;  
  7.     private String r;  
  8.   
  9.     public String getR() {  
  10.         return r;  
  11.     }  
  12.   
  13.     public void setR(String r) {  
  14.         this.r = r;  
  15.     }  
  16.   
  17.     public int getType() {  
  18.         return type;  
  19.     }  
  20.   
  21.     public void setType(int type) {  
  22.         this.type = type;  
  23.     }  
  24.   
  25.     @Override  
  26.     public String execute() throws Exception {  
  27.         if (type == 1) {  
  28.             r = "/user_success.jsp";  
  29.         } else if (type == 2) {  
  30.             r = "/user_error.jsp";  
  31.         }  
  32.         return "success";  
  33.     }  
  34. }  
  35.   
  36.  <body>  
  37.     动态结果一定不要往了为动态结果的保存设置set get方法  
  38. <ol>  
  39.     <li><a href="user/user?type=1">返回seuccess</a></li>  
  40.     <li><a href="user/user?type=2">返回error</a></li>  
  41. </ol>  
  42.   </body>  
  43.   
  44. <package name="user" namespace="/user" extends="struts-default">  
  45.         <action name="user" class="com.demo.action.UserAction">  
  46.             <result>${r}</result>  
  47.         </action>  
  48.     </package>   
  49.   
  50. User Success !!!  
  51.     <s:property value="r"/>  
  52.     <s:debug></s:debug>  

  1. package com.demo.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class UserAction extends ActionSupport{  
  6.     private int type;  
  7.   
  8.     public int getType() {  
  9.         return type;  
  10.     }  
  11.   
  12.     public void setType(int type) {  
  13.         this.type = type;  
  14.     }  
  15.       
  16.     @Override  
  17.     public String execute() throws Exception {  
  18.         return SUCCESS;  
  19.     }  
  20. }  

  1. <package name="user" namespace="/user" extends="struts-default">  
  2.       
  3.     <action name="user" class="com.demo.action.UserAction">  
  4.         <result type="redirect">/user_success.jsp?t=${type}</result>  
  5.     </action>       
  6.    </package>  

  1. 向结果传参数  
  2. <ol>  
  3.     <li>  
  4.         <a href="user/user?type=1">传参数</a>  
  5.     </li>  
  6. </ol>  
  1. <body>  
  2. User Success !!<br/>  
  3. from valuestack: <s:property value="t"/><br/>  
  4. from actioncontext: <s:property value="#prameters.t"/>  
  5. <s:debug></s:debug>  
  6.  </body>  



  1. User Success !!  
  2. from valuestack:   
  3. from actioncontext: 1   
  4. [Debug] 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值