David, in your example, there is no need to add the "userId" to the url.
Since forward passes the entire request to the page that's being forwarded
to, and userId is already part of the request query, your next page will
automatically get the user id that was passed into the action.  Thus if you
know that the "successPage", or "nextPage" of your action will require
userId, simply use an <html:hidden...> to pass the value to the action.

Andrew's example is different.  He "grabs" a value out of thin air, and
wishes to add it to the parameters of the current request.  In my
development I try to avoid doing such things.  For example, suppose there is
a page which accepts userName (friendly string), and uses that name to
display something about that user.  One way of doing this is:

Option 0: Non struts way
------------------------
1. Page accepts user name ("alex") and executes UserLookupAction
2. UserLookupAction finds the id of the user 2112 and forwards to the
DisplayUser.jsp?userId=2112
3. DisplayUser.jsp uses a <jsp:useBean to get the details about the user and
display full information.

In the above example, UserLookupAction would need to append "?userId=2112"
to the list of parameters passed to the DisplayUser.jsp page.  A more
"struts" way of doing this as well as avoiding having to append parameters
to the request, would be:

Option 1: Configure bean in to the request
--------------------------------------------------
1. Page accepts user name ("alex) and executes UserLookupAction
2. UserLookupAction finds user "alex" in the database and creates a java
bean via parameter.setAttribute() to represent such user
3. DisplayUser.jsp assumes that a particular request attribute is already
set in the request and just proceeds to display the data

Option 2: Avoids call to parameter.setAttribute(...)
----------------------------------------------------
1. Create a UserLookupActionForm form bean to contain a field called
userName:String and userInfo:UserBean
2. Configure UserLookupAction to use form UserLookupActionForm, here you can
specify that the form should be in request
3. Some JSP page accepts user name ("alex") and executes UserLookupAction
   a) Struts automatically creates UserLookupActionForm
   b) It is placed in the scope you have indicated
   c) It is passed into the UserLookupAction already filled in with the
userName typed in on the page before
4. UserLookupAction gets the "form" and extracts the userName out of it
5. Using the userName it finds the UserBean in the database and sets it back
in to the userInfo:UserBean property of the form
6. Next page can assumes that the form is in the request, and proceeds to
display the userInfo:UserBean structure

In both of these cases the Action is what finds the necessary information
and does the "setup" so that the page (view) is only responsible for
displaying the data.  This has worked for me well.

Good luck.


-AP_
http://www.myprofiles.com/member/profile/apara_personal

-----Original Message-----
From: Andrew Hill [mailto:[EMAIL PROTECTED]]
Sent: Saturday, July 06, 2002 7:06 AM
To: Struts Users Mailing List
Subject: RE: Return an ActionForward with additional query data?


I find I have a few places in my code that look like this:

String path =
StaticWebUtils.addParameterToURL(forward.getPath(),"uid","12345");
forward = new ActionForward(path,true); // Create new ActionForward with
appropriate params added to path

(the method it uses just determines whether to use ? or &)

public static String addParameterToURL(String url, String parameter, String
value)
{
   return url + ( (url.indexOf("?")==-1) ? "?" : "&" ) + parameter + "=" +
value;
}

I too wonder if there is a better way of doing this.


-----Original Message-----
From: David Mulligan [mailto:[EMAIL PROTECTED]]
Sent: Friday, July 05, 2002 19:38
To: '[EMAIL PROTECTED]'
Subject: Return an ActionForward with additional query data?



> Is it possible to return an ActionForward with additional query
> data/parameters that may arise in the Business logic of the system?
>
> public class UserPrefAction extends org.apache.struts.action.Action {
>       public ActionForward perform(......) {
>               String userId = request.getParameter("userId");
>               //Do some database stuff here
>               .....
>               .....
>               //If success, then forward to the User Display page which is
> '/settings/userDisplay.do?userId=xxxxx'
>               return new ActionForward("success");
>       }
> }
>
>
> If my struts-config.xml
>
>     <action path="/settings/userPref"
>             type=".......UserPrefAction"
>             scope="request"
>             input="/settings/userPref.vm">
>       <forward name="success" path="/settings/userDisplay.do"/>
>     </action>
>
>
>
> The problem is that I don't know what the userId is until the
> UserPrefAction is preformed.
> How do/Can I create an ActionForward with query data that may arise in the
> Business logic?
>
> Would it be logical to extend the struts framework to do use the
> java.util.Message.format and do something like
>
> In the XML
>
>       <forward name="success" path="/settings/userDisplay.do?userId=$1"/>
>
> In the Java code
>
>       ActionForward forward = new ActionForward("success");
>       forward.bind(userId);
>       return forward;
>
Where $1 would be replaced with the toString() value of the first object
that is bound to the ActionForward using .bind(Object ojb)


> I know it's possible to save the userId in the HttpSession, but I don't
> really
> want to take that path. I know as soon as I allow general access to
> HttpSession
> it will be used to save all types of objects between transactions.
>
> Thanks in advance.
> Dave.

--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>


--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to