wood burning stoves 2.0*
The moose likes Struts and the fly likes Loading multiple forms in single jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Loading multiple forms in single jsp" Watch "Loading multiple forms in single jsp" New topic
Author

Loading multiple forms in single jsp

vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Hi,

My jsp page has multiple forms (one form for each table which has specific info). So assume it has 3 tables, so 3 forms.

I need to load all the form when the page is loaded.



So if I need to load all the forms, how should I proceed. Also please note each form has its own form class and action class.

Any help would be greatly appreciated.

Thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by vjy chin:
I need to load all the form when the page is loaded.

If by "loaded" you mean the data in the forms populated by ActionForm beans, then you don't need to do anything at all to make this happen. All the data in all the forms will be loaded when the page is displayed. Struts and the web container will handle this automatically.

If you need to populate multiple ActionForm beans prior to displaying the page, you can do so. Just instantiate the beans, populate them, and put them in the proper scope. So, for example, if you specified request scope for each action mapping, you could do the following:

However, you need to understand that when the user presses a submit button, only the form that encloses that particular submit button will be submitted. The other two will not.
[ February 06, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Thanks for the reply.

I am not in my office now, so will try out your suggestion tomorrow.

By loading I meant, populating the table values from database. So I can call a service class in the reset method of each form, populate each bean and set it in the request. AM I right?

I will try out and let you know tomorrow.

Thanks for the suggestion.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Yes, you could populate each Form in the reset() method and it would work. This solution does present problems, though. Struts will call the reset method before displaying the JSP, which is great, but when the form is submitted, Struts will call the reset() method again, which in your example will result in unnecessary database access.

I think it's cleaner to create a preparatory action that loads all the forms and then forwards to the JSP. That way you are in complete control of when the database access code is called and when it isn't.
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Thanks For the reply again. But can you explain this a bit more
I think it's cleaner to create a preparatory action that loads all the forms and then forwards to the JSP


How can I call an action without any events? I mean

For e.g. Assume Page A (From Page A I am coming to this form page via a hyperlink and there is no action class in between) calls the page with 3 forms and I need to display all the values in the 3 forms. So how can I call the action classes for each of the 3 forms.


So when and how will the action1, action2, action3 called?

Thanks
[ February 07, 2007: Message edited by: vjy chin ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
From Page A I am coming to this form page via a hyperlink and there is no action class in between

Well there is your answer...you need an action class in between! Rather than having a hyperlink directly to a JSP, you would use a hyperlink to an action. This action would be responsible for instantiating, populating and saving to the request (or session) all three action forms. In general I like to think that every JSP file needs two actions...one to display the page and one to process the submission of the page.

- Brent
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Thanks Brent,

I was also thinking of the same, using an action class in between, but in the requirement doc, it was given as normal hyperlink. So got that sorted out now.

Using action class in between i am able to load the data.

Thanks for your time.
subha nair
Greenhorn

Joined: Jun 19, 2008
Posts: 23
could you please explain how you have done it.
I am having a page. I need to load data from database when the page loads.
how can we do it..
please help its urgent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading multiple forms in single jsp
 
Similar Threads
Dynamic Jsp coding
Multiple form with one action
Info: Memorizing Deployment Descriptor
Setting the path of servlet and jsp pages
content of dropdown B will depend on what is selected on dropdown A