It not pretty, but in large web applications, you need to pass around data in the URL from time to time. Usually it has to do with passing something about the state of the application. Its either this or put it in the session. I don't like the session because:
- Everything you put in the session can bog down performance if you need to replicate your session for failover.
- Its too hard to keep track of whats in the session. Users can jump around your application and easily jumble things up.
/*
* ParameterizedActionForward.java
*
* Created on August 3, 2001, 11:45 AM
*/
package org.apache.struts.action;
import java.net.URLEncoder;
/**
*
* @author Jan Soresen
* @version
*/
public class ParameterizedActionForward extends ActionForward {
/** Create a new ParameterizedActionForward,
* populated from an ActionForward
* @param forward Populate the receiver from this ActionForward
*/
public ParameterizedActionForward(ActionForward forward) {
setPath(forward.getPath());
setRedirect(forward.getRedirect());
firstParameter = getPath().indexOf('?') == -1;
}
/** Create a new ParameterizedActionForward,
* based on an ActionForward found in an ActionMapping
* @param mapping The ActionMapping to get the base forward from
* @param forwardName The ActionMapping to get the base forward from
*/
public ParameterizedActionForward(ActionMapping mapping, String forwardName) {
this(mapping.findForward(forwardName));
}
/** Create a new ParameterizedActionForward
* @param forward The base ActionForward
* @param key The key of the parameter
* @param value The value of the parameter
*/
public ParameterizedActionForward(ActionForward forward, String key, String value)
{
this(forward);
addParameter(key, value);
}
/** Create a new ParameterizedActionForward
* @param mapping The ActionMapping to get the base forward from
* @param forwardName The ActionMapping to get the base forward from
* @param key The key of the parameter
* @param value The value of the parameter
*/
public ParameterizedActionForward(ActionMapping mapping, String forwardName,
String key, String value) {
this(mapping, forwardName);
addParameter(key, value);
}
/** Add a new parameter
* @param key The key of the parameter
* @param value The value of the parameter
*/
public void addParameter(String key, String value) {
StringBuffer buffer = new StringBuffer(getPath());
buffer.append(firstParameter ? '?' : '&apm;');
firstParameter = false;
buffer.append(URLEncoder.encode(key));
buffer.append('=');
buffer.append(URLEncoder.encode(value));
setPath(buffer.toString());
}
private boolean firstParameter;
}
With this class you can write code like this:
ParameterizedActionForward forward =
new ParameterizedActionForward( mapping.findForward("updateSuccess"), "foo", "bar" );
return forward;
Its a shame, because there has been a bug submitted for this for over 3 years. It even has a good patch similar to the file above, complete with testcases. Why hasn't it been added to CVS? I'm not sure, but I know how you can help. Exercise your right to vote on this bug!
<< Home