Parameters in configuration results

  1. Parameters in configuration results   
  2.  Edit Page    Browse Space    Add Page    Add News    
  3. Added by Dariusz Wojtas, last edited by Dariusz Wojtas on Feb 03, 2007  (view change)    
  4. Sometimes there is a need to redirect from one action to another, but you do not know the exact url or the destination url requires parameters that are only known in runtime.    
  5.   
  6. Struts 2 gives you easy to use solution for that problem.   
  7.   
  8. Parameters in action result definitions   
  9. <struts>  
  10. ....   
  11.    <package name="somePackage" namespace="/myNamespace" extends="struts-default">  
  12.       <action name="myAction" class="com.project.MyAction">  
  13.          <result name="success" type="redirect-action">otherAction?id=${id}result>  
  14.          <result name="back" type="redirect">${redirectURL}result>  
  15.       action>  
  16.   
  17.       <action name="otherAction" class="com.project.MyOtherAction">  
  18.          ...   
  19.       action>         
  20.    package>  
  21. ....   
  22. struts>  
  23. The only requirement is to declare the necessary properties in your action, in this case com.project.MyAction should define properties id and redirectURL with standard accessor methods.   
  24.   
  25. public class MyAction extends ActionSupport {   
  26.    private int id;   
  27.    private String redirectURL;   
  28.    ...   
  29.   
  30.   
  31.    public String execute() {   
  32.        ...   
  33.       if (someCondition) {   
  34.          this.redirectURL = "/the/target/page.action";   
  35.          return "back";   
  36.       }   
  37.   
  38.       this.id = 123;   
  39.       return SUCCESS;    
  40.    }   
  41.   
  42.    public int getId() { return this.id; }   
  43.    public void setId(int id) { this.id = id; }   
  44.    public String getRedirectURL() { return this.redirectURL; }   
  45.    public void setRedirectURL(String redirectURL) { this.redirectURL= redirectURL; }   
  46.    ...   
  47. }   
  48. In the above code if it returns SUCCESS then the browser will be forwarded to   
  49. /<app-prefix>/myNamespace/otherAction.action?id=123  
  50.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值