File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes read value from URL and process on page (keep mvc pattern) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "read value from URL and process on page (keep mvc pattern)" Watch "read value from URL and process on page (keep mvc pattern)" New topic
Author

read value from URL and process on page (keep mvc pattern)

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Hi all,

Here is the deal: I have a registration page which the user has to fill out - http://localhost/registration.jsp . The user may �arrive� from a different URL to this page (example user clicks the link: http://localhost/registration.jsp?id=996

The id is the value I would like to retrieve in order to show different values on the registration.jsp page. For example, with id 996 � show a truck and title, with 443 � show an SUV and info.

Question: how can I construct the page based on the id values? The problem I�ve encounter is that the page is processed (Action) only after the user has clicked submit.

With the given id (before reaching the registration.jsp) , I will have to go to the db fetch some info and plug it back to the registration page. I don�t wish to do that (action) in the jsp because I wish to keep the MVC pattern.

One thought I had in mind is to use an intermediate page / intermediate.jsp?id=996
that will be redirected to the registration.jsp with the id value and beans (but I wonder if there�s a better way to do that)

thanks for any thought
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
I believe mapping can be tweaked to have registration page request processed through action servlet, If that doesn't work then you have to use http-equiv meta redirect in the registration jsp page to make the request to an action.
See http://www.html-reference.com/META_httpequiv_refresh.htm on how to redirect from client side.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Instead of forwarding directly to the jsp, I would use a "display" action instead. Forward to that action and have the code there do the processing you need. Your url would then look something like http://localhost/displayRegistration.do?id=996

- Brent
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Brent,
If I understand you correctly, you're saying I should have a �previous page�: displayRegister that takes the id and fw it to the registration.jsp.

Basically, in between I can have displayRegisterAction - get the ID and make any init, setup I want. Right?

1. Is this the process you had in mind?
2. if displayRegister is just a redirect do I still need to create for it a bean? or in other words, what's the shortest way to get to the displayRegisterAction, read the id and fw to registration.jsp
3. what code will be in the displayRegister.jsp to ignite displayRegisterAction?

Thanks
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
#1 - Yea, I think that is what I had in mind.

#2 - Just a redirect? I was under the impression that you would need code to read a record from the database to populate the jsp page. I am picturing that your action would look like this:


#3 - I am not sure what you are asking here. The file displayRegister.jsp will probably submit to an action that saves the registration data (maybe saveRegistration.do).

- Brent
[ October 17, 2006: Message edited by: Brent Sterling ]
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Thanks!

I did that and it works!

So it works like this: user clicks a url with displayRegistration?id=344
and that is redirected to registration.jsp with the id.
in-between I do some business logic in the displayRegistrationAction.

the only thing, which i suspect to be a bit incorrect, is my struts-config action-mapping:

I did this;

path=displayRegistration
type=com.action.displayRegistrationAction
name=myForm //??? this belongs to something else
scope=request
input=/displayRegistration
validate=false
.
.
.

the displayRegistration.jsp has NOTHING in it - so it goes directly to the displayRegistrationAction (great)-> business --> fw to the registration with attribute

my question is this: do you think the mapping-action for this action is ok? I mean I don't use any form, no need to validate and NO BEAN!
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
If you are not using a form bean, you can delete the form attribute from your action definition (just delete that entire line). The form parameter that gets passed into the execute method will be null.

- Brent
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
thanks Brent, you tought me something new today.
(oh...new icon)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: read value from URL and process on page (keep mvc pattern)
 
Similar Threads
Problem with Faces Servlet
forward to another page from a jsp page
need help with basic spring mvc program... please help
problem in navigation rule
Restricted forums are not so restricted [FIXED]