ITeye博客

Parameters in configuration results

Allen.CD.China · 2007-10-10 17:12 · Struts · Struts · Markdown

  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. Struts 2 gives you easy to use solution for that problem.
  6. Parameters in action result definitions
  7. <struts>
  8. ....
  9. <package name="somePackage" namespace="/myNamespace" extends="struts-default">
  10. <action name="myAction" class="com.project.MyAction">
  11. <result name="success" type="redirect-action">otherAction?id=${id}result>
  12. <result name="back" type="redirect">${redirectURL}result>
  13. action>
  14. <action name="otherAction" class="com.project.MyOtherAction">
  15. ...
  16. action>
  17. package>
  18. ....
  19. struts>
  20. 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.
  21. public class MyAction extends ActionSupport {
  22. private int id;
  23. private String redirectURL;
  24. ...
  25. public String execute() {
  26. ...
  27. if (someCondition) {
  28. this.redirectURL = "/the/target/page.action";
  29. return "back";
  30. }
  31. this.id = 123;
  32. return SUCCESS;
  33. }
  34. public int getId() { return this.id; }
  35. public void setId(int id) { this.id = id; }
  36. public String getRedirectURL() { return this.redirectURL; }
  37. public void setRedirectURL(String redirectURL) { this.redirectURL= redirectURL; }
  38. ...
  39. }
  40. In the above code if it returns SUCCESS then the browser will be forwarded to
  41. /<app-prefix>/myNamespace/otherAction.action?id=123

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。