Personally I would like to see this better supported
by Struts, and there is also an enhancement request in
the bug database (number 866). 

I have attached a class (ParameterizedActionForward)
where you can write something like:

return new ParameterizedActionForward(
    mapping, "forwardName", "paramName", paramValue)

There are some other contructors, and you can also add
parameters after the ParameterizedActionForward has
been created.

It will properly handle if the forward already
contains parameter, and encoding of the key and value.

- Jan Sorensen

--- Martin Cooper <[EMAIL PROTECTED]>
wrote:
> You can use the <forward> in struts-config.xml as a
> base value and append your parameters dynamically.
> Something like this:
> 
>     ActionForward fwd = mapping.findForward("blue");
>     StringBuffer path = new
> StringBuffer(fwd.getPath());
>     path.append("?tableID=")
>         .append(tableID); // where this might have
> the value "foo"
>     return new
> ForwardingActionForward(path.toString());
> 
> --
> Martin Cooper
> 
> 
> 
> -----Original Message-----
> From:    David Corbin [EMAIL PROTECTED]
> Sent:    Sun, 26 Aug 2001 21:07:03 -0400
> To:      [EMAIL PROTECTED]
> Subject: Re: Dynamic forwardings...
> 
> 
> I'm not sure where I'm miscommunication, so I'll go
> back to the beginning.
> 
> I have an action "list".  It expects an parameter
> "tableID", so a link to it
> looks like "http://host/app/list.do?tableID=foo";,
> and if things are
> successful, returns a page with a list of items that
> depend on the tableID.
> For each item, there is a link generated for action
> "blue" that specifies
> two parameters: tableID & itemID
> (http://host/app/blue.do?tableID=foo&itemID=xyzzy).
> 
> No problem.
> 
> The action "blue" requires the two parameters
> "tableID" and "itemID".  Under
> certain circumstances, the results of the action
> blue require that
> "list.do?tableID=foo" be "forwarded".  Struts
> doesn't seem to provide a way
> to handle this last case.  If it does, I don't
> understand it.
> 
> David
> ----- Original Message -----
> From: "Ted Husted" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Sunday, August 26, 2001 7:14 PM
> Subject: Re: Dynamic forwardings...
> 
> 
> > Typically, there would be a finite number of
> inputs, and you could have
> > a logical forward for each one.
> >
> > The ActionMappings form an API for the legal entry
> points to the
> > application.
> >
> > Usually, the only thing that is truly dynamic is a
> reference into a data
> > source, to look something up. In that case, the
> link tag can take one or
> > more dynamic parameters, based on the collection
> of beans used to write
> > it.
> >
> > In that case, the action usually gets the
> parameters from the request,
> > and uses them to look up whatever is required.
> >
> > David Corbin wrote:
> > >
> > > I'm definately talking about "forward", but my
> "whatever" value is
> dynamic.
> > > It's based on input (a parameter) into the
> action.  I don't yet
> understand
> > > the best solution for that.
> > >
> > > ----- Original Message -----
> > > From: "Ted Husted" <[EMAIL PROTECTED]>
> > > To: <[EMAIL PROTECTED]>
> > > Sent: Sunday, August 26, 2001 2:56 PM
> > > Subject: Re: Dynamic forwardings...
> > >
> > > > For a forward, you just put in an encoded
> query string for the path:
> > > >
> > > >      <forward name="itemAdd"
> > > >           path="/do/item/Add?code=whatever"/>
> > > >
> > > > For an action mapping, you can use the
> parameter property
> > > >
> > > >             <action
> > > >                 path="/do/item/Add"
> > > > ...
> > > >                 parameter="add">
> > > >             </action>
> > > >
> > > > and have the action check for the code there
> > > >
> > > > String code = mapping.getParameter();
> > > >
> > > >
> > > > David Corbin wrote:
> > > > >
> > > > > I've got a an action "add", which requires a
> an argument "code".
> > > > > One (at least) of the possible forwards
> needs to specify this "code"
> on
> > > the
> > > > > forward URL.
> > > > >
> > > > > How can I do that?
> > > > >
> > > > > Thanks
> > > > > David Corbin
> >
> >
> 
> 
> 
> 
> 
> 
>
___________________________________________________________________________
> Visit http://www.visto.com.
> Find out  how companies are linking mobile users to
> the 
> enterprise with Visto.
> 


__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/
/*
 *  ParameterizedActionForwardTestCase.java
 *
 * Created on August 3, 2001, 1:45 PM
 */

package org.apache.struts.action;

import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;

/**
 *
 * @author  Jan Sorensen
 * @version 
 */
public class ParameterizedActionForwardTestCase extends TestCase {

    /** Creates new ParameterizedActionForwardTestCase
     * @param name  */
    public ParameterizedActionForwardTestCase(String name) {
        super(name);
    }

    /**
     * Set up instance variables required by this test case.
     */
    public void setUp() {
        mapping = new ActionMapping();
        ActionForward forward = new ActionForward("/command.do");
        forward.setName("test");
        mapping.addForward(forward);
    }
    
    /**
     * Tear down instance variables required by this test case.
     */
    public void tearDown() {
        mapping = null;
    }
    
    /**
     * Return the tests included in this test suite.
     * @return A test suite (<code>TestSuite</code>) that includes all methods 
starting with "test"
     */
    public static Test suite() {
        return (new TestSuite(ParameterizedActionForwardTestCase.class));
    }
    
    /** Veriry that <CODE>ParameterizedActionForward</CODE> are created properly
     */    
    public void testCreation() {
        ActionForward f;
        
        f = new ParameterizedActionForward(mapping, "test");
        verify(f, "/command.do");
        
        f = new ParameterizedActionForward(mapping, "test", "key", "value");
        verify(f, "/command.do?key=value");
        
        f = new ParameterizedActionForward(mapping.findForward("test"));
        verify(f, "/command.do");
        
        f = new ParameterizedActionForward(mapping.findForward("test"), "key", 
"value");
        verify(f, "/command.do?key=value");
    }

    /** Verify that adding parameters works
     */    
    public void testAddParameter() {
        ParameterizedActionForward f;
        
        f = new ParameterizedActionForward(mapping, "test");
        verify(f, "/command.do");
        
        f.addParameter("key1", "value1");
        verify(f, "/command.do?key1=value1");
        f.addParameter("key2", "value2");
        verify(f, "/command.do?key1=value1&key2=value2");
    }
    
    /** Verify the encoding of parameter keys and values
     */    
    public void testUrlEncoding() {
        ParameterizedActionForward f;
        
        f = new ParameterizedActionForward(mapping, "test", "k e y", "?&=+");
        verify(f, "/command.do?k+e+y=%3F%26%3D%2B");
    }
        
    private void verify(ActionForward f, String path) {
        assertNull("Name is null", f.getName());
        assertEquals("Path ok", path, f.getPath());
    }
    
    private ActionMapping mapping;
        
}
/*
 * 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 <CODE>ParameterizedActionForward</CODE>, 
     * populated from an <CODE>ActionForward</CODE> 
     * @param forward Populate the receiver from this <CODE>ActionForward</CODE>
     */
    public ParameterizedActionForward(ActionForward forward) {
        setPath(forward.getPath());
        setRedirect(forward.getRedirect());
        firstParameter = getPath().indexOf('?') == -1;
    }
    
    /** Create a new <CODE>ParameterizedActionForward</CODE>, 
     * based on an <CODE>ActionForward</CODE> found in an <CODE>ActionMapping</CODE>
     * @param mapping The <CODE>ActionMapping</CODE> to get the base forward from
     * @param forwardName The <CODE>ActionMapping</CODE> to get the base forward from
     */
    public ParameterizedActionForward(ActionMapping mapping, String forwardName) {
        this(mapping.findForward(forwardName));
    }
    
    /** Create a new <CODE>ParameterizedActionForward</CODE>
     * @param forward The base <CODE>ActionForward</CODE>
     * @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 <CODE>ParameterizedActionForward</CODE>
     * @param mapping The <CODE>ActionMapping</CODE> to get the base forward from
     * @param forwardName The <CODE>ActionMapping</CODE> 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 ? '?' : '&');
        firstParameter = false;
        buffer.append(URLEncoder.encode(key));
        buffer.append('=');
        buffer.append(URLEncoder.encode(value));
        setPath(buffer.toString());
    }
    
    private boolean firstParameter;
    
    
}

Reply via email to