Struts 2: Action Redirects

Bookmark and Share
Struts 2.0 provides the ability to chain multiple actions into a defined sequence or workflow. Action chaining can be achieved in multiple ways:
  • Action Chains: This feature works by applying a test:Chain Result to a given Action, and intercepting its target Action's invocation with a ChainingInterceptor.
  • Redirect After Post: A common a pattern in web application development in which your action will result in a redirect to another action.
    • Redirect Action Result: Uses ActionMapper provided by the ActionMapperFactory to redirect the browser to a URL that invokes the specified action. You can see a simple implementation of this in the struts-blank application. If you look at the example.xml file in the application you will can see that the Login action redirects the result to a different action "Menu".
      <action name="Login!*" method="{1}" class="example.Login">
      <result name="input">/example/Login.jsp</result>
      <result type="redirect-action">Menu</result>
      </action>
      In order to send parameters, use the <param></param> tag, as shown below
      <param name="paramname">value</param>
    • Redirect Result:Calls theHttpServletResponse.sendRedirect(), (a browser redirect). The consequence of doing this means that the action (action instance, action errors, field errors, etc) that was just executed is lost and no longer available. This is because actions are built on a single-thread model. The only way to pass data is through the session or with web parameters (?name=value). In the following code, you can see that the "Menu.Action" is used instead of just Menu.
      <action name="Login!*" method="{1}" class="example.Login">
      <result name="input">/example/Login.jsp</result>
      <result type="redirect">Menu.action</result>
      </action>

{ 0 comments... Views All / Send Comment! }

Post a Comment