Struts 2.3 - redirect vs redirectAction

Jaikrat picture Jaikrat · Sep 25, 2013 · Viewed 8.8k times · Source

What is the main difference between redirect and redirectAction in Struts2.3 context.

I have seen below URLs for redirect and redirectAction.

I am clear about below points:

  • redirect is like sendRedirect() method. New request is created which clear the previous value stack and action (action instance, action errors, field errors, etc) no longer available.
  • In redirectAction, control jumps to the different action(in same or other package)
  • redirectAction is recommended over redirect.

But when I implemented both above mentioned examples, I only had to change my struts.xml.

  • In both, action is no longer available,
  • New request is created,
  • Generated URL was same.

First, I am confused with the statement written in Apache documentation of redirectAction

This is better than the ServletRedirectResult because it does not require you to encode the URL patterns processed by the ActionMapper in to your struts.xml configuration files. This means you can change your URL patterns at any point and your application will still work. It is strongly recommended that if you are redirecting to another action, you use this result rather than the standard redirect result.

Second, I am still not very much clear about the difference between these both.

Answer

Roman C picture Roman C · Sep 25, 2013

The difference is obvious: redirectAction is just another result type, which extends redirect result type providing the attributes actionName, namespace, and method. These attributes used to build the URI used to substitute for the location attribute of the redirect result type.

The statement is ok, better build URL from ActionMapping which is implemented by the redirectAction result type instead of coding it manually. Even manually always use the UrlHelper instead of hardcoding. In the JSP always use the s:url tag.