Struts Redirect w/Parameters

January 24, 2006

If you have a Struts Action servlet and you want to redirect to another page, the standard Struts technique is to return an ActionForward and setup an appropriate forward entry in struts-config.xml:

return mapping.findForward("success");

Unfortunately, this doesn’t provide a mechanism for passing request parameters to the target. So what can you do if you want to redirect to another page and pass some parameters along? You use an additional class:

public class ForwardParameters
{
private Map params = null;

public ForwardParameters()
{
params = new HashMap();
}

/**
* Add a single parameter and value.
*
* @param paramName     Parameter name
* @param paramValue    Parameter value
*
* @return A reference to this object.
*/
public ForwardParameters add(String paramName, String paramValue)
{
params.put(paramName,paramValue);
return this;
}

/**
* Add a set of parameters and values.
*
* @param paramMap  Map containing parameter names and values.
*
* @return A reference to this object.
*/
public ForwardParameters add(Map paramMap)
{
Iterator itr = paramMap.keySet().iterator();
while (itr.hasNext())
{
String paramName = (String) itr.next();
params.put(paramName, paramMap.get(paramName));
}

return this;
}

/**
* Add parameters to a provided ActionForward.
*
* @param forward    The ActionForward object to add parameters to.
*
* @return ActionForward with parameters added to the URL.
*/
public ActionForward forward(ActionForward forward)
{
StringBuffer path = new StringBuffer(forward.getPath());

Iterator itr = params.entrySet().iterator();
if (itr.hasNext())
{
//add first parameter, if avaliable
Map.Entry entry = (Map.Entry) itr.next();
path.append("?" + entry.getKey() + "=" + entry.getValue());

//add other parameters
while (itr.hasNext())
{
entry = (Map.Entry) itr.next();
path.append("&" + entry.getKey() + "=" + entry.getValue());
}
}

return new ActionForward(path.toString());
}
}

Here are some examples of using ForwardParameters:

Long form:

ActionForward forward = mapping.findForward("success");
ForwardParameters fwdParams = new ForwardParameters();
fwdParams.add("myparam", "myvalue");
fwdParams.add("something", "fornothing");
return fwdParams.forward(forward);

Concise form:

ActionForward forward = mapping.findForward("success");
return new ForwardParameters().add("myparam", "myvalue").add("something", "fornothing").forward(forward);

Ultra concise form:

return new ForwardParameters().add("myparam", "myvalue").add("something", "fornothing").forward(mapping.findForward("success"));

This is based on a suggestion I found on Experts-Exchange by dualsoul.

EDIT: This article has been superseded by a newer article.

10 Responses to “Struts Redirect w/Parameters”

  1. Bryan Says:

    Thanks for the tip.. I was wondering about this just now and found your site.. Off to implement it!

  2. Albert Says:

    Works very well, thanks a lot!!
    Will adding parameters to an ActionForward be added in an upcoming Struts release?


  3. As someone just pointed out to me this morning, you can use the ActionRedirect class instead of my ForwardParameters class. According to the JavaDocs, ActionRedirect was added in Struts 1.2.7 and is “a subclass of ActionForward which is designed for use in redirecting requests, with support for adding parameters at runtime.” I’ll be posting a new blog entry soon to suggest this class instead of mine.


  4. [...] In an earlier post, I described a class to handle redirects in Struts and passing parameters along. That technique is not necessary; as of Struts 1.2.7, you can use the ActionRedirect class instead. [...]

  5. mady Says:

    i try to find something at google.com and take it on your site…thanks

  6. Tobias Schoessler Says:

    Thanks for the code, was very usefull. Though there is a problem with the ActionForards you retrun in the forward methode. The forward returned is no redirect forward anymore as you create it by constructor and do not configure it to be a redirect. setRedirect(true) in the line before retruning the forward fixes this.

  7. John Doe from Portugal Says:

    Freakin’ life saver!!!

  8. Juan Perez Says:

    Good, but you also have to replace especial chars like spaces, latin accents etc, in your parameters

  9. Marcel Says:

    Man, thanks a lot!
    I was having many problems because of that and you solve them!

    Thanks

  10. Mario Says:

    It seems so easy but i never was used something like that.
    Now my Struts developer live is much more straightforward.
    Thanks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.