This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Struts and the fly likes Struts 1 :- Two Form submission from one jsp using one submit button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 1 :- Two Form submission from one jsp using one submit button" Watch "Struts 1 :- Two Form submission from one jsp using one submit button" New topic
Author

Struts 1 :- Two Form submission from one jsp using one submit button

Siddhesh Tawde
Greenhorn

Joined: Nov 03, 2009
Posts: 4
Hi, this is my 1st post...

I am facing problem in submitting two forms from one JSP on one submit button..
I am using Struts 1.1, MySql 5.1 and Tomcat 6.20
Here is the detailed senario...

1. I have 2 tables Warehouse and Address in my MySql db,
2 . Warehouse table has address_id field as a foreign key which is a Auto_increment value in address table
3. I have a jsp called createWarehouse.jsp which contains two forms for warehouse and address with one submit button in address form,
4. I use a javascript to submit these two forms
5. As address id is the foreign key i need to persist address details 1st, so i set the action for address form and submit it through javascript
6. Then i take the address_id put it in a session object and refer it while persisting warehouse details.
7. Then from the same js function i submit warehouse form too
8. Every thing goes fine if i put a alert() statement between two form submissions
9. But if i remove that alert statement only address gets persisted

Here is wht i have figured out : In case if i don't put the alert statement in between.
1. When address form is submitted it goes to actionclass then to the daobean class and the queries are executed there,
2. But simultaneously the control goes forward in jsp file it goes to submit the warehouse form, this both process are going on parallely
3. And as in warehouse action it doesn't get the address_id from session it doesn't persist in warehouse table

But if i put the alert statement there is a pause between two submission and the address form submission gets enough time to get session object containing address_id.

I don't want to use window.timeout() as the transaction time can vary, even tried to put a while loop until i get the session attribute but dint work
Are my finding correct ???....
Please tell me a way around....Hoping to hear from Experts


Code is given below:--

JavaScript:-


createWarehouse.jsp :-


struts-config.xml


Awaiting Reply..Thanks in Advance
Cheers
S.!.D


Freedom discovers man the moment he looses concern of what Impression he is making or is about to make.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

I would recommend you to reconsider your design. I've personally never came across a situation where I had to submit two different forms from one page. Why are you actually submitting the form to two different actions. Can you design one action that will process both the forms (or to say just one form that will contain both the warehouse and address information). That action will first persist the warehouse object, and then use its id to persist the address object...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Siddhesh Tawde
Greenhorn

Joined: Nov 03, 2009
Posts: 4
Hi Ankit, Thanks for replying,

Well thats the easy way, but thats not feasible for my project as Address table is a common table for different masters like warehouse is the one and others are users, customers, transporters, complex, consignors and so on....
and thats why I have different ActionForms and ActionClasses for all of them...

I am also using Ajax in some places, is there any way to get through this using ajax ??

Cheers
S.!.D
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

The logic to insert records into the address table should be in a separate class i.e. not an action. Something like DAO should be used to do database handling. So it should not be a problem to use the DAO class in two actions. Still if you want to stick to your pattern, then the Warehouse Action can insert the warehouse row in the database and itself forward the request to the address action. So here too there will be only one form that will be submitted to the warehouse action that will contain both warehouse and address data. The warehouse action will use the warehouse data and the address action will use the address data...
Siddhesh Tawde
Greenhorn

Joined: Nov 03, 2009
Posts: 4
You are right, i have a DAO class, 1st when the address form is submitted it goes to a Action class which sends it to DAO to persist and in the same DAO it retrives the address_id which is kept in a session object to be refered by warehouse DAO while persisting warehouse data
I have one ActionClass with two different methods for the job and there are two DAOclasses, one for each
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Should I consider the problem solved??
Siddhesh Tawde
Greenhorn

Joined: Nov 03, 2009
Posts: 4
Noo...The Problem is still there....
I just told you the structure.....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

And I told you the solution. Post all the information to the address action which will insert an address record and then forward the request to warehouse action (although I personally would've not used this approach but you insisted on inserting address records in address action)...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts 1 :- Two Form submission from one jsp using one submit button