You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Swaminathan Rajagopalan <RS...@infosys.com> on 2004/02/09 09:33:50 UTC

Need to modify the URL path of the forward dynamically

Hi,

We have a URL in the path attribute of a forward for an action. We need to dynamically append some request variables to this URL in the action class, for they are available only at execution time. How can this be achieved?

E.g
 <action    path="/additionalAccountInfoScreen"
               type="xxx.yyy.zzz.web.control.action.AdditionalAccountInfoScreenAction"
               name="additionalAccountInfoForm"
               validate="false"
               input="previousPage"
               parameter="populateStmt"
               scope="session">
	<forward name="url"              path="http://www.xyz.com/checks/"

To the URL path here e.g http://www.xyz.com/checks we need to add accessId=xxx at run time in the action class(http://www.xyz.com/checks?accessId=xxx).

Please let us know how this can be done?

Regards,
Swaminathan Rajagopalan,
Ph : (8520261) Extn: 55955
Mail : rswami@infosys.com 


Re: Need to modify the URL path of the forward dynamically

Posted by John <gr...@johnsteele.com>.
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20830927.html

This has also been discussed on the user list.

Swaminathan Rajagopalan wrote:

> Hi,
>
> We have a URL in the path attribute of a forward for an action. We 
> need to dynamically append some request variables to this URL in the 
> action class, for they are available only at execution time. How can 
> this be achieved?
>
> E.g
>  <action    path="/additionalAccountInfoScreen"
>                
> type="xxx.yyy.zzz.web.control.action.AdditionalAccountInfoScreenAction"
>                name="additionalAccountInfoForm"
>                validate="false"
>                input="previousPage"
>                parameter="populateStmt"
>                scope="session">
>         <forward name="url"              
> path="http://www.xyz.com/checks/"
>
> To the URL path here e.g _http://www.xyz.com/checks_ we need to add 
> accessId=xxx at run time in the action 
> class(http://www.xyz.com/checks?accessId=xxx).
>
> Please let us know how this can be done?
>
> /Regards,/
> /Swaminathan Rajagopalan,/
> /Ph : (8520261) Extn: 55955/
> /Mail : rswami@infosys.com/
>
>------------------------------------------------------------------------
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-dev-help@jakarta.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org


R: R: repost: validator & retrieving data

Posted by Leonardo Francalanci <lf...@simtel.ie>.
> The input for the save action is the second action or the jsp 
> page if you use a filter.
> Am i missing something?

No! Yu're right! Why didn't I think about it!?

THANK YOU

(grazie...)

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: R: repost: validator & retrieving data

Posted by Michele Callegari <mc...@open1.it>.
> Ok. So an action prepares the collection for the select.
> The other one initializes the form with data from DB.
> 
> To which one should I set the input property of the "save" action?
> If I set it to the second one (which initializes the form) I will
> have the same problem... should this action retrieve data from the db
> or simply leave it as is because it contains invalid data entered by
> the user?
> 
> I think having 2 actions doesn't help here...

- The first action just initializes the form
- The second action prepares the collection for the select.
  (This is just an helper for the viewer, it can even be a filter)

The input for the save action is the second action or the jsp 
page if you use a filter.
Am i missing something?

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


R: repost: validator & retrieving data

Posted by Leonardo Francalanci <lf...@simtel.ie>.
> I think it's right only in simple cases
> If your "prepare" means:
> - Putting in the request a collection for a select (for example)
> - Initializing the form with data from DB or any other source
> You are doing 2 things completely different (logically)
> in the same action and that's why you need a workaround 
> with hidden fields or hard coded path or....
> 
> The cleanest solution is to separate this "prepare" action in 
> 2 actions. Too much? 

Ok. So an action prepares the collection for the select.
The other one initializes the form with data from DB.

To which one should I set the input property of the "save" action?
If I set it to the second one (which initializes the form) I will
have the same problem... should this action retrieve data from the db
or simply leave it as is because it contains invalid data entered by
the user?

I think having 2 actions doesn't help here...


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: repost: validator & retrieving data

Posted by Michele Callegari <mc...@open1.it>.
> "If there are 2 actions, one to "prepare" the page and 
> one to "process" the user input, we define the input attribute 
> value of the "process" action mapping to be the URL of the 
> "prepare" action." I think it's right,....

I think it's right only in simple cases
If your "prepare" means:
- Putting in the request a collection for a select (for example)
- Initializing the form with data from DB or any other source
You are doing 2 things completely different (logically)
in the same action and that's why you need a workaround 
with hidden fields or hard coded path or....

The cleanest solution is to separate this "prepare" action in 
2 actions. Too much? 
OK! Choose any other way you like! But it won't be "clean".

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: Need to modify the URL path of the forward dynamically

Posted by Andrew Hill <an...@gridnode.com>.
btw:
stringNotEmpty(parameter)
just does a check to see that the parameter isnt null or ""

-----Original Message-----
From: Andrew Hill [mailto:andrew.david.hill@gridnode.com]
Sent: Monday, 9 February 2004 17:25
To: Struts
Subject: RE: Need to modify the URL path of the forward dynamically


>From what Ive seen on the list, the way we are using seems to be pretty much
standard practice.
I did see some activity on the dev list regarding enhancing forwards to make
doing this sort of thing easier, though I wasnt following closely so cant
tell you what the outcome was.

If its something you need to do a lot you may wish to create a method in
your base action class that will take the name of a forward, some parameters
to tack on, and go look up the forward and return a new instance with the
parameters added to the path. I dont bother with that myself though. I do
have some methods to deal with the ? vs & when tacking on the url.

Heres the code (2 methods) I use to do the appending (they live in a static
class where I dump lots of odds and ends like this!):

ummm... the encoder bit... hmmm. You could live without that if you dont try
to append funny chars. :-) hehe. The original version of the method was a
one-liner.

public static String addParameterToURL(String url, String parameter, String
value)
  {
    try
    {
      if(value != null) value = URLEncoder.encode(value, "UTF-8");
    }
    catch(UnsupportedEncodingException e)
    {
      throw new NestableRuntimeException("Error encoding value "
                                          + quotedString(value),e);
    }
    if(url == null)
    {
      return parameter + "=" + value;
    }
    else
    {
      return url + ( (url.indexOf("?")==-1) ? "?" : "&" ) + parameter + "="
+ value;
    }
  }


/**
   * Uses propertyutils to read parameters from source. Empty/null
parameters are NOT appended.
   */
  public static String addParametersToURL(String url, Object source,
String[] parameters)
  {
    if( source == null ) return url;
    if( parameters == null || parameters.length == 0 ) return url;
    StringBuffer buffer = new StringBuffer(url == null ? "" : url);
    for( int i = 0; i < parameters.length; i++ )
    {
      String parameter = parameters[i];
      if( stringNotEmpty(parameter) )
      {
        String value = null;
        try
        {
          value = stringValue( PropertyUtils.getProperty(source,
parameter) );
        }
        catch(Throwable t)
        {
          value = null;
        }
        if( StaticUtils.stringNotEmpty(value) )
        {
          buffer.append( buffer.indexOf("?")==-1 ? "?" : "&" );
          buffer.append(parameter);
          buffer.append("=");
          try
          {
            buffer.append(URLEncoder.encode(value, "UTF-8"));
          }
          catch(UnsupportedEncodingException e)
          {
            throw new NestableRuntimeException("Error encoding value "
                                                + quotedString(value),e);
          }
        }
      }
    }
    return buffer.toString();
  }


-----Original Message-----
From: Max Cooper [mailto:max@maxcooper.com]
Sent: Monday, 9 February 2004 17:24
To: Struts Users Mailing List; andrew.david.hill@gridnode.com
Subject: Re: Need to modify the URL path of the forward dynamically


Andrew's procedure is what my team has done on one project, too. Originally,
we tried getting the ActionForward and changing it directly, but it turns
out that is a shared instance and changing the original was messing up other
requests. After we discovered this problem, we switched to Andrew's scheme
of creating a new ActionForward to return from the Action.

I posted a message today with a note about dynamically adding a query string
to an ActionForward in the "URL does not change even after changing action"
thread. This thread has the details about how to do that.

I am interested to hear from other folks if they have a novel way to do this
(i.e. add a query string to an ActionForward). Anybody got a better way?

-Max

----- Original Message -----
From: "Andrew Hill" <an...@gridnode.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Monday, February 09, 2004 12:36 AM
Subject: RE: Need to modify the URL path of the forward dynamically


> Need to modify the URL path of the forward dynamicallyGet the Forward
> instance as usual. Obtain its url string using getPath(). Add your
> additional parameter to end of string and create a new ActionForward
> instance that uses the decorated path (and copies the redirect property
from
> the original action forward), and return this new actionforward instance.
>   -----Original Message-----
>   From: Swaminathan Rajagopalan [mailto:RSwami@infosys.com]
>   Sent: Monday, 9 February 2004 16:34
>   To: struts-dev@jakarta.apache.org; struts-user@jakarta.apache.org
>   Cc: Kamal_Poddar
>   Subject: Need to modify the URL path of the forward dynamically
>
>
>   Hi,
>
>   We have a URL in the path attribute of a forward for an action. We need
to
> dynamically append some request variables to this URL in the action class,
> for they are available only at execution time. How can this be achieved?
>
>   E.g
>    <action    path="/additionalAccountInfoScreen"
>
> type="xxx.yyy.zzz.web.control.action.AdditionalAccountInfoScreenAction"
>                  name="additionalAccountInfoForm"
>                  validate="false"
>                  input="previousPage"
>                  parameter="populateStmt"
>                  scope="session">
>           <forward name="url"
path="http://www.xyz.com/checks/"
>
>   To the URL path here e.g http://www.xyz.com/checks we need to add
> accessId=xxx at run time in the action
> class(http://www.xyz.com/checks?accessId=xxx).
>
>   Please let us know how this can be done?
>
>   Regards,
>   Swaminathan Rajagopalan,
>   Ph : (8520261) Extn: 55955
>   Mail : rswami@infosys.com
>
>



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


RE: Need to modify the URL path of the forward dynamically

Posted by Andrew Hill <an...@gridnode.com>.
>From what Ive seen on the list, the way we are using seems to be pretty much
standard practice.
I did see some activity on the dev list regarding enhancing forwards to make
doing this sort of thing easier, though I wasnt following closely so cant
tell you what the outcome was.

If its something you need to do a lot you may wish to create a method in
your base action class that will take the name of a forward, some parameters
to tack on, and go look up the forward and return a new instance with the
parameters added to the path. I dont bother with that myself though. I do
have some methods to deal with the ? vs & when tacking on the url.

Heres the code (2 methods) I use to do the appending (they live in a static
class where I dump lots of odds and ends like this!):

ummm... the encoder bit... hmmm. You could live without that if you dont try
to append funny chars. :-) hehe. The original version of the method was a
one-liner.

public static String addParameterToURL(String url, String parameter, String
value)
  {
    try
    {
      if(value != null) value = URLEncoder.encode(value, "UTF-8");
    }
    catch(UnsupportedEncodingException e)
    {
      throw new NestableRuntimeException("Error encoding value "
                                          + quotedString(value),e);
    }
    if(url == null)
    {
      return parameter + "=" + value;
    }
    else
    {
      return url + ( (url.indexOf("?")==-1) ? "?" : "&" ) + parameter + "="
+ value;
    }
  }


/**
   * Uses propertyutils to read parameters from source. Empty/null
parameters are NOT appended.
   */
  public static String addParametersToURL(String url, Object source,
String[] parameters)
  {
    if( source == null ) return url;
    if( parameters == null || parameters.length == 0 ) return url;
    StringBuffer buffer = new StringBuffer(url == null ? "" : url);
    for( int i = 0; i < parameters.length; i++ )
    {
      String parameter = parameters[i];
      if( stringNotEmpty(parameter) )
      {
        String value = null;
        try
        {
          value = stringValue( PropertyUtils.getProperty(source,
parameter) );
        }
        catch(Throwable t)
        {
          value = null;
        }
        if( StaticUtils.stringNotEmpty(value) )
        {
          buffer.append( buffer.indexOf("?")==-1 ? "?" : "&" );
          buffer.append(parameter);
          buffer.append("=");
          try
          {
            buffer.append(URLEncoder.encode(value, "UTF-8"));
          }
          catch(UnsupportedEncodingException e)
          {
            throw new NestableRuntimeException("Error encoding value "
                                                + quotedString(value),e);
          }
        }
      }
    }
    return buffer.toString();
  }


-----Original Message-----
From: Max Cooper [mailto:max@maxcooper.com]
Sent: Monday, 9 February 2004 17:24
To: Struts Users Mailing List; andrew.david.hill@gridnode.com
Subject: Re: Need to modify the URL path of the forward dynamically


Andrew's procedure is what my team has done on one project, too. Originally,
we tried getting the ActionForward and changing it directly, but it turns
out that is a shared instance and changing the original was messing up other
requests. After we discovered this problem, we switched to Andrew's scheme
of creating a new ActionForward to return from the Action.

I posted a message today with a note about dynamically adding a query string
to an ActionForward in the "URL does not change even after changing action"
thread. This thread has the details about how to do that.

I am interested to hear from other folks if they have a novel way to do this
(i.e. add a query string to an ActionForward). Anybody got a better way?

-Max

----- Original Message -----
From: "Andrew Hill" <an...@gridnode.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Monday, February 09, 2004 12:36 AM
Subject: RE: Need to modify the URL path of the forward dynamically


> Need to modify the URL path of the forward dynamicallyGet the Forward
> instance as usual. Obtain its url string using getPath(). Add your
> additional parameter to end of string and create a new ActionForward
> instance that uses the decorated path (and copies the redirect property
from
> the original action forward), and return this new actionforward instance.
>   -----Original Message-----
>   From: Swaminathan Rajagopalan [mailto:RSwami@infosys.com]
>   Sent: Monday, 9 February 2004 16:34
>   To: struts-dev@jakarta.apache.org; struts-user@jakarta.apache.org
>   Cc: Kamal_Poddar
>   Subject: Need to modify the URL path of the forward dynamically
>
>
>   Hi,
>
>   We have a URL in the path attribute of a forward for an action. We need
to
> dynamically append some request variables to this URL in the action class,
> for they are available only at execution time. How can this be achieved?
>
>   E.g
>    <action    path="/additionalAccountInfoScreen"
>
> type="xxx.yyy.zzz.web.control.action.AdditionalAccountInfoScreenAction"
>                  name="additionalAccountInfoForm"
>                  validate="false"
>                  input="previousPage"
>                  parameter="populateStmt"
>                  scope="session">
>           <forward name="url"
path="http://www.xyz.com/checks/"
>
>   To the URL path here e.g http://www.xyz.com/checks we need to add
> accessId=xxx at run time in the action
> class(http://www.xyz.com/checks?accessId=xxx).
>
>   Please let us know how this can be done?
>
>   Regards,
>   Swaminathan Rajagopalan,
>   Ph : (8520261) Extn: 55955
>   Mail : rswami@infosys.com
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Need to modify the URL path of the forward dynamically

Posted by Max Cooper <ma...@maxcooper.com>.
Andrew's procedure is what my team has done on one project, too. Originally,
we tried getting the ActionForward and changing it directly, but it turns
out that is a shared instance and changing the original was messing up other
requests. After we discovered this problem, we switched to Andrew's scheme
of creating a new ActionForward to return from the Action.

I posted a message today with a note about dynamically adding a query string
to an ActionForward in the "URL does not change even after changing action"
thread. This thread has the details about how to do that.

I am interested to hear from other folks if they have a novel way to do this
(i.e. add a query string to an ActionForward). Anybody got a better way?

-Max

----- Original Message ----- 
From: "Andrew Hill" <an...@gridnode.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Monday, February 09, 2004 12:36 AM
Subject: RE: Need to modify the URL path of the forward dynamically


> Need to modify the URL path of the forward dynamicallyGet the Forward
> instance as usual. Obtain its url string using getPath(). Add your
> additional parameter to end of string and create a new ActionForward
> instance that uses the decorated path (and copies the redirect property
from
> the original action forward), and return this new actionforward instance.
>   -----Original Message-----
>   From: Swaminathan Rajagopalan [mailto:RSwami@infosys.com]
>   Sent: Monday, 9 February 2004 16:34
>   To: struts-dev@jakarta.apache.org; struts-user@jakarta.apache.org
>   Cc: Kamal_Poddar
>   Subject: Need to modify the URL path of the forward dynamically
>
>
>   Hi,
>
>   We have a URL in the path attribute of a forward for an action. We need
to
> dynamically append some request variables to this URL in the action class,
> for they are available only at execution time. How can this be achieved?
>
>   E.g
>    <action    path="/additionalAccountInfoScreen"
>
> type="xxx.yyy.zzz.web.control.action.AdditionalAccountInfoScreenAction"
>                  name="additionalAccountInfoForm"
>                  validate="false"
>                  input="previousPage"
>                  parameter="populateStmt"
>                  scope="session">
>           <forward name="url"
path="http://www.xyz.com/checks/"
>
>   To the URL path here e.g http://www.xyz.com/checks we need to add
> accessId=xxx at run time in the action
> class(http://www.xyz.com/checks?accessId=xxx).
>
>   Please let us know how this can be done?
>
>   Regards,
>   Swaminathan Rajagopalan,
>   Ph : (8520261) Extn: 55955
>   Mail : rswami@infosys.com
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: Need to modify the URL path of the forward dynamically

Posted by Andrew Hill <an...@gridnode.com>.
Need to modify the URL path of the forward dynamicallyGet the Forward
instance as usual. Obtain its url string using getPath(). Add your
additional parameter to end of string and create a new ActionForward
instance that uses the decorated path (and copies the redirect property from
the original action forward), and return this new actionforward instance.
  -----Original Message-----
  From: Swaminathan Rajagopalan [mailto:RSwami@infosys.com]
  Sent: Monday, 9 February 2004 16:34
  To: struts-dev@jakarta.apache.org; struts-user@jakarta.apache.org
  Cc: Kamal_Poddar
  Subject: Need to modify the URL path of the forward dynamically


  Hi,

  We have a URL in the path attribute of a forward for an action. We need to
dynamically append some request variables to this URL in the action class,
for they are available only at execution time. How can this be achieved?

  E.g
   <action    path="/additionalAccountInfoScreen"

type="xxx.yyy.zzz.web.control.action.AdditionalAccountInfoScreenAction"
                 name="additionalAccountInfoForm"
                 validate="false"
                 input="previousPage"
                 parameter="populateStmt"
                 scope="session">
          <forward name="url"              path="http://www.xyz.com/checks/"

  To the URL path here e.g http://www.xyz.com/checks we need to add
accessId=xxx at run time in the action
class(http://www.xyz.com/checks?accessId=xxx).

  Please let us know how this can be done?

  Regards,
  Swaminathan Rajagopalan,
  Ph : (8520261) Extn: 55955
  Mail : rswami@infosys.com