You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Tommy Holm - TELMORE <to...@telmore.dk> on 2004/03/16 13:51:30 UTC
SV: What is the best way to pass a parameter to a forward? Create a utility class .Client code
Forgot to show the code how to use it!
In your action you should not return
return mapping.findForward("bla")
but
Hashtable a = new Hashtable();//add what ever key/value pairs you need
return new
ActionForwardParameters().add(a).forward(mapping.findForward("success"))
;
-----Oprindelig meddelelse-----
Fra: Tommy Holm - TELMORE
Sendt: 16. marts 2004 13:48
Til: Struts Users Mailing List
Emne: SV: What is the best way to pass a parameter to a forward? Create
a utility class
Some time ago this question has been asked before and someone showed
some utility class code. I used the code and created a class like this:
import java.util.HashMap; import java.util.Hashtable; import
java.util.Iterator; import java.util.Map;
import org.apache.struts.action.ActionForward;
/**
* Simple utility class to add parameters to an ActionForward
* from within an action. This action is useful when you from
* within an action need to pass parameters to the jsp page or another
action
* you are forwarding to.
*/
public class ActionForwardParameters {
/**
* Encupsulates parameters for ActionForward.
*/
private Map params = new HashMap();
/**
* add all the parameters and values from the hashtable to the
actionforward
* @param parametersValues a hastable with key value pairs
* @return ActionForwardParameters object
*/
public ActionForwardParameters add(Hashtable parametersValues) {
for(Iterator i =
parametersValues.keySet().iterator();i.hasNext();){
String key = (String) i.next();
params.put(key,(String) parametersValues.get(key));
}
return this;
}
/**
* Add parameters to provided ActionForward
* @param forward ActionForward to add parameters to
* @return ActionForward with added parameters to URL
*/
public ActionForward forward(ActionForward forward) {
StringBuffer path = new StringBuffer(forward.getPath());
Iterator iter = params.entrySet().iterator();
if (iter.hasNext()) {
//add first parameter, if avaliable
Map.Entry entry = (Map.Entry) iter.next();
path.append("?" + entry.getKey() + "=" + entry.getValue());
//add other parameters
while (iter.hasNext()) {
entry = (Map.Entry) iter.next();
path.append("&" + entry.getKey() + "=" +
entry.getValue());
}
}
return new ActionForward(path.toString());
}
}
Please note that I didn't come up with the idea of this class, I simply
created the class after reading the information that a fellow programmer
Provided.!
-----Oprindelig meddelelse-----
Fra: Jay Glanville [mailto:jay.glanville@naturalconvergence.com]
Sendt: 16. marts 2004 13:13
Til: 'Struts Users Mailing List'
Emne: RE: What is the best way to pass a parameter to a forward?
It's funny that you say that it will not work, because it does for me,
and without throwing any exceptions.
That being said, your point about modifying an action mappings forwards
is a good one. It is unfortunate that the ActionForward class doesn't
have a copy constructor ....
--
Jay Glanville
> -----Original Message-----
> From: news [mailto:news@sea.gmane.org] On Behalf Of Martin Cooper
> Sent: Monday, March 15, 2004 8:30 PM
> To: struts-user@jakarta.apache.org
> Subject: Re: What is the best way to pass a parameter to a forward?
>
>
> What you are doing will not work - at least, not the way you are doing
> it. You are trying to modify the ActionForward instance that is
> owned by Struts,
> and calling setPath() on that instance will result in an
> IllegalStateException.
>
> You need to create your own ActionForward instance, instead of trying
> to modify Struts' one. You can do that with something like this:
>
> ActionForward goto = mapping.findForward( "success" );
> String path = ...; // Put together your new path
> ActionForward myGoto = new ActionForward(path,
> goto.getRedirect());
> return myGoto;
>
> --
> Martin Cooper
>
>
> "Glanville, Jay" <Ja...@NaturalConvergence.com> wrote in
> message
> news:827BF123F7B64D48A7F305D668884075815881@ex2k.bitheadsinc.local...
> I'm trying to solve a problem, but I'm not sure my solution
> is the best
> way. Basically, I want to set a parameter on a forward within the
> action's execute.
>
> I'm in my action's execute method. I've just successfully performed
> some work, and I now want to forward/redirect to the next page. So,
> I've got some code that looks like this:
>
> public ActionForward execute(ActionMapping.....) {
> // do some work
> ActionForward goto = mapping.findForward( "success" );
> return goto;
> }
>
> With an action mapping that looks like this:
>
> <action
> path="/EntrySave"
> type="com.package.EntrySaveAction"
> name="EntryForm"
> validate="true"
> input="/Entry.do">
> <forward name="success" redirect="true" path="/Container.do"/>
> <forward name="failure" redirect="false" path="/Entry.do" />
> </action>
>
> Basically, if I left the EntrySaveAction.execute do what's doing right
> now, then upon successful completion, it would redirect to
> "/Container.do". However, what I want it to do is redirect to
> "/Container.do?id=45", where 45 is the id of the container that I want
> to go to. The value of "45" is dynamic, and is know at the time of
> the EntrySaveAction.execute command.
>
> The way I'm currently doing it is something like this:
>
> public ActionForward execute(ActionMapping.....) {
> // do some work
> ActionForward goto = mapping.findForward( "success" );
> String path = goto.getPath();
> path += "?id=" + container.getId();
> goto.setPath( path );
> return goto;
> }
>
> Is this the correct way to do this? Is there a better way?
>
> Thanks in advance
>
> JDG
>
>
> --
> Jay Glanville
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org