7

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Jaikrat
  • 1,124
  • 3
  • 24
  • 45
  • 4
    `redirect` is redirecting to some URL external to application context say 'google' or `yahoo` where as `redirectAction` is redirecting within the scope of your application.Also in all cases a new request will be generated. – Umesh Awasthi Sep 25 '13 at 11:57
  • 1
    Why as comment, this is the answer, clear and straight ;) – Andrea Ligios Sep 25 '13 at 12:32

2 Answers2

2

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 10
    If it was obvious there would be no need to ask the question :-) – buzzsawddog Feb 20 '14 at 16:38
  • if you have a question you may ask it to the tag, in other words your comment has less sense to the answer I provided long time ago. You could also upvote this and OP posts as usual. – Roman C Feb 20 '14 at 18:16
  • 1
    Merriam-Webster defines obvious as "easily discovered, seen, or understood". I don't think the difference is obvious. Nevertheless, I appreciate finding the answer here. – simon Feb 06 '17 at 10:36
2

One redirects to actions, one redirects to arbitrary resources.

actionRedirect is better for most Struts 2 applications since most redirects will happen to locations inside the application, and all you need to supply is the action name from its mapping.

You could do the same thing with a plain redirect, but you'd need to supply the action extension, if any, so it's a bit uglier and a bit ore fragile (e.g., if the action extension changes, although that happens rarely).

Dave Newton
  • 158,873
  • 26
  • 254
  • 302