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,
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:--
Awaiting Reply..Thanks in Advance
Freedom discovers man the moment he looses concern of what Impression he is making or is about to make.
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...
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 ??
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...
Joined: Nov 03, 2009
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
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)...
subject: Struts 1 :- Two Form submission from one jsp using one submit button