Apache Struts 2 Documentation > Home > FAQs > Parameters in configuration results
Added by Dariusz Wojtas, last edited by Dariusz Wojtas on Feb 03, 2007  (view change)

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.

Struts 2 gives you easy to use solution for that problem.

Parameters in action result definitions

<struts>
....
   <package name="somePackage" namespace="/myNamespace" extends="struts-default">
      <action name="myAction" class="com.project.MyAction">
         <result name="success" type="redirect-action">otherAction?id=${id}</result>
         <result name="back" type="redirect">${redirectURL}</result>
      </action>

      <action name="otherAction" class="com.project.MyOtherAction">
         ...
      </action>      
   </package>
....
</struts>

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.

public class MyAction extends ActionSupport {
   private int id;
   private String redirectURL;
   ...


   public String execute() {
       ...
      if (someCondition) {
         this.redirectURL = "/the/target/page.action";
         return "back";
      }

      this.id = 123;
      return SUCCESS; 
   }

   public int getId() { return this.id; }
   public void setId(int id) { this.id = id; }
   public String getRedirectURL() { return this.redirectURL; }
   public void setRedirectURL(String redirectURL) { this.redirectURL= redirectURL; }
   ...
}

In the above code if it returns SUCCESS then the browser will be forwarded to
/<app-prefix>/myNamespace/otherAction.action?id=123