struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bill Clinton <bclin...@snipermail.com>
Subject Re: URL Encode for a JSP Page using Struts
Date Fri, 24 Aug 2001 21:33:42 GMT
Ted,
    Maybe I am mistaken, but I think he wants to add the parameters in 
the perform method of his Action.  So in the case the html:link tag 
might not work.

For cases like these (and I am open to better suggestions) I have 
extended the Action Forward class to make a ParameterActionForward 
class.  Here is the code:

########### ParameterActionForward.java ##########

import org.apache.struts.action.ActionForward;
import java.util.HashMap;
import java.util.Iterator;

/**
  * Implementation of <strong>ActionForward</strong> that allows 
requestParameters
  * added on to the path.
  *
  * @author Bill Clinton
  * @version $Id: ParameterActionForward.java,v 1.1 2001/08/17 15:24:42 
bclinton Exp $
  */
public final class ParameterActionForward extends ActionForward {


     private HashMap parameters = new HashMap();
     private String  path;

     private static final String questionMark = "?";
     private static final String ampersand = "&";
     private static final String equals = "=";

     public ParameterActionForward(ActionForward forward) {

         setName(forward.getName());
         setPath(forward.getPath());
         setRedirect(forward.getRedirect());

     }

     public void addParameter(String paramName, Object paramValue) {

         addParameter(paramName,paramValue.toString());

     }

     public void addParameter(String paramName, String paramValue) {

         parameters.put(paramName,paramValue);

     }

     public void setPath(String path) {

         this.path=path;

     }

     public String getPath() {

         StringBuffer sb = new StringBuffer();
         sb.append(path);

         boolean firstTimeThrough = true;
         if (parameters!=null && !parameters.isEmpty()) {

             sb.append(questionMark);

             Iterator it = parameters.keySet().iterator();
             while (it.hasNext()) {

                 String paramName = (String)it.next();
                 String paramValue = (String)parameters.get(paramName);

                 if (firstTimeThrough) {
                     firstTimeThrough=false;
                 } else {
                     sb.append(ampersand);
                 }
                 sb.append(paramName);
                 sb.append(equals);
                 sb.append(paramValue);

             }
         }
         return sb.toString();
     }
}
########### End ParameterActionForward.java ##########


This is an example of using the class to add two parameters to a forward:

ParameterActionForward forward = new ParameterActionForward(
	mapping.findForward("success"));
forward.addParameter("id",String.valueOf(questionForm.getQuestionId()));
forward.addParameter("modified","true");
return forward;


I hope this is useful to you Krishna.  And I welcome any (constructive) 
peer criticism.  In fact, as I was pasteing, I noticed a potential bug. 
I don't think this would work on forwards that didn't have redirect set 
to true, so play with it, test it, and modify it before you use it.  I 
am using it for something specific and it works for me.

Bill





Ted Husted wrote:

> Then you would use a html:link tag with the actionForm as the source.
> 
> http://jakarta.apache.org/struts/struts-html.html#link
> 
> Bhamidi Krishna wrote:
> 
>> Hi Ted,
>> 
>> thankyou for the mail,
>> 
>> but what if mode and value (either one or both) keep getting generated
>> dynamically dependent on the actionForm ?
>> 
>> Krishna
>> 
>> 
>>> Just put it in as a Global Forward, something like this
>>> 
>>>      <forward name="itemFindScript"
>>>           path="/do/item/Search?column=script"/>
>>> 
>>> or in your case
>>> 
>>>      <forward name="something"
>>>           path="/do/Something?mode=21"/>
>>> 
>>> or, using extension mapping
>>> 
>>>      <forward name="something"
>>>           path="/Something.do?mode=21"/>
>>> 
>>> 
>>> You can also refer to Global Forwards in the link tag, and Struts will
>>> append any other parameters you supply (paramID, paramProperty).
>>> 
>>> Bhamidi Krishna wrote:
>>> 
>>>> Hi,
>>>> 
>>>> I have an elementary question. I want to encode the URL of my jsp page
>>> 
>>> that
>>> 
>>>> I call using findForward() say something like a mode parameter.
>>>> 
>>>> So my jsp url will look like:
>>>> http://localhost:8080/someURL/doSomething?mode=21
>>>> 
>>>> How can I do that?
>>>> 
>>>> Krishna
>>>> 
>>>> _________________________________________________________________
>>>> Get your FREE download of MSN Explorer at
>>> 
>>> http://explorer.msn.com/intl.asp
>>> 
>>> -- Ted Husted, Husted dot Com, Fairport NY USA.
>>> -- Custom Software ~ Technical Services.
>>> -- Tel +1 716 737-3463
>>> -- http://www.husted.com/about/struts/
>> 
>> _________________________________________________________________
>> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
> 
> 
> -- Ted Husted, Husted dot Com, Fairport NY USA.
> -- Custom Software ~ Technical Services.
> -- Tel +1 716 737-3463
> -- http://www.husted.com/about/struts/


Mime
View raw message