Blind Squirrel

Once in a while, even a blind squirrel finds a nut.

Struts Redirect w/Parameters

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:

1
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:

ForwardParameters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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:

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

Concise form:

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

Ultra concise form:

1
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 June 5th, 2006:

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.

JavaDocs