28

I'm trying to have my Struts2 app redirect to a generated URL. In this case, I want the URL to use the current date, or a date I looked up in a database. So /section/document becomes /section/document/2008-10-06

What's the best way to do this?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Sietse
  • 7,884
  • 12
  • 51
  • 65

6 Answers6

65

Here's how we do it:

In Struts.xml, have a dynamic result such as:

<result name="redirect" type="redirect">${url}</result>

In the action:

private String url;

public String getUrl()
{
 return url;
}

public String execute()
{
 [other stuff to setup your date]
 url = "/section/document" + date;
 return "redirect";
}

You can actually use this same technology to set dynamic values for any variable in your struts.xml using OGNL. We've created all sorts of dynamic results including stuff like RESTful links. Cool stuff.

Johnny Wey
  • 916
  • 7
  • 6
  • 1
    Thanks a lot, that works nicely! Is there some way to do the change to the xml so that it doesn't need to be applied to each and every action i've got? I'd ideally like that to be applicable to all my actions. – Chris Nov 24 '09 at 04:51
  • 2
    You might try a global result. I haven't experimented with this for dynamic variables, but, as long as the action returns the result, I don't see any reason it wouldn't work. – Johnny Wey Dec 04 '09 at 18:12
15

One can also use annotations and the Convention plug-in to avoid repetitive configuration in struts.xml:

@Result(location="${url}", type="redirect")

The ${url} means "use the value of the getUrl method"

KNU
  • 2,560
  • 5
  • 26
  • 39
Ivan Morales
  • 151
  • 1
  • 3
3

If anyone wants to redirect directly in ActionClass:

public class RedirecActionExample extends ActionSupport {
HttpServletResponse response=(HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);

    url="http://localhost:8080/SpRoom-1.0-SNAPSHOT/"+date;
    response.sendRedirect(url);
    return super.execute(); 
}

Edit: Added a missing quote.

Tom11
  • 2,419
  • 8
  • 30
  • 56
hari
  • 1,874
  • 1
  • 16
  • 10
  • What are the advantages or disadvantages of redirecting inside the action class itself, rather then using struts.xml for this? I can already see one difference: by using struts.xml, redirecting is actually two completely separate requests back to back, whereas your solution is only one request? – user1884155 Nov 05 '14 at 14:55
2

I ended up subclassing Struts' ServletRedirectResult and overriding it's doExecute() method to do my logic before calling super.doExecute(). it looks like this:

public class AppendRedirectionResult extends ServletRedirectResult {
   private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

  @Override
  protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
    String date = df.format(new Date());
    String loc = "/section/document/"+date;
    super.doExecute(loc, invocation);
  }
}

I'm not sure if this is the best way to do it, but it works.

Sietse
  • 7,884
  • 12
  • 51
  • 65
1

You can redirect to another action using annotation -

@Result(
    name = "resultName",
    type = "redirectAction",
    params = { "actionName", "XYZAction" }
)
Stephan
  • 41,764
  • 65
  • 238
  • 329
tiwari.vikash
  • 367
  • 3
  • 6
1

One can redirect directly from an interceptor without regard to which action is involved.

In struts.xml

    <global-results>
        <result name="redir" type="redirect">${#request.redirUrl}</result>
    </global-results>

In Interceptor

@Override
public String intercept(ActionInvocation ai) throws Exception
{
    final ActionContext context = ai.getInvocationContext();        
    HttpServletRequest request = (HttpServletRequest)context.get(StrutsStatics.HTTP_REQUEST);
    request.setAttribute("redirUrl", "http://the.new.target.org");
    return "redir";
}
Aaron
  • 874
  • 3
  • 17
  • 34