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.



May 25, 2006 at 2:14 pm
Thanks for the tip.. I was wondering about this just now and found your site.. Off to implement it!
June 5, 2006 at 8:18 am
Works very well, thanks a lot!!
Will adding parameters to an ActionForward be added in an upcoming Struts release?
June 5, 2006 at 8:31 am
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.
June 5, 2006 at 8:44 am
[...] 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. [...]
July 21, 2006 at 3:30 am
i try to find something at google.com and take it on your site…thanks
August 28, 2006 at 10:18 am
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.
October 17, 2008 at 10:39 am
Freakin’ life saver!!!
March 6, 2009 at 8:56 am
Good, but you also have to replace especial chars like spaces, latin accents etc, in your parameters
March 8, 2009 at 12:16 pm
Man, thanks a lot!
I was having many problems because of that and you solve them!
Thanks
March 12, 2009 at 1:13 pm
It seems so easy but i never was used something like that.
Now my Struts developer live is much more straightforward.
Thanks