wood burning stoves 2.0*
The moose likes Struts and the fly likes Combining two Forms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Combining two Forms" Watch "Combining two Forms" New topic

Combining two Forms

M Mehta
Ranch Hand

Joined: Aug 03, 2006
Posts: 92

I have a problem in combining two <html:form>. I had a single JSP with with different sections and one submit button. I split the JSP into two using tiles. Now I am showing the two JSP and the lower one has the submit button. Now I want the values from both the JSP's should be submitted on the click of that single button. How do I achieve this?

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You can do it with Javascript. Suppose you have formA and formB, and formB has the submit button. You can do something like this:

Note: onnsubmit above should be onsubmit. I had to misspell it to get past the JavaRanch filters.

Having said this, be aware that you may get some strange behavior. What's going to happen is that two different server threads will be spawned, and both of them will send a response back to the browser. It's anybody's guess which one will hit the browser first.

The only time I've used this technique is when each form was in a different frame within the same frameset, so they each returned to different frames and didn't interfere with each other. Tiles doesn't work that way. Even though Tiles makes the page look like it's divided into different frames, there's still only one actual frame.

Granted I don't understand what you're trying to do in your application, but at this point, I'm having a hard time believing you really need two different forms. If you're submitting them both at the same time anyway, it sounds to me like one would do.
[ February 13, 2007: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
M Mehta
Ranch Hand

Joined: Aug 03, 2006
Posts: 92
Thanks Merrill for the reply !!

I am using two different JSP's because there are some scenarios where I dont want to show the second JSP, only the first one.

Also as I am using <html:form> so I am not sure that I can use name attribute there. In the ActionMappings I have associated same FormBean for both the JSP, so they both have the same name.

Is there any way I can refer to the form of another JSP in my current jsp? I think that will help me to solve my problem.

mohit bahl
Ranch Hand

Joined: Mar 22, 2006
Posts: 48
hi Mehta

i might not have got your problem correctly but i will take a shot. correct me if i am wrong. you have JSP1 and JSP2. JSP2 in some circumstances might not be visible but JSP1 will always be visible.
instead of creating 2 separate formbeans you can use only one which has all the attributes of both the jsps. what you have to do now is on submit just call the action which you want depending on the situation.

hope this helps


to err is human, but the company policy doesn't allow it!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Me Mehta:
Is there any way I can refer to the form of another JSP in my current jsp?

You mentioned in your first post that you're using Tiles to display the two JSPs together on the same page. If this is true, there is still only one physical HTML document containing two different forms. Struts automatically assigns the name of the form as the name of the ActionForm bean assigned to the Action you've declared for the form, so you're right, the name of both forms will be the same.

However, in JavaScript there is always more than one way to refer to something. If you know there are only two forms on the page, you can refer to them as elements of an array like this:


Alternately if you specify styleId="x" in the <html:form> tag, Struts will format that as id="x" when the actual html is rendered. If you do this, you can refer to the form like this:

var myForm = document.getElementById("x");

So yes, you can refer to one form from the other as long as they're physically present in the same document.

Having answered your question, here's how I'd do it: I say that the fact you want some information to be displayed sometimes and not others is not a good enough reason to split it into two separate forms. Using CSS attributes you can make part of the page invisible even though it's still there in the document. I'd suggest keeping everything in the same JSP and the same form, but using CSS attributes to hide part of the document. You can then use JavaScript code to un-hide it when appropriate. Example:

When you want to un-hide it, execute the following JavaScript code:

For example, suppose you only want to display part B when your ActionForm bean property "displayPartB", which is of type boolean is true. Just put the following at the end of the document just before the </body> tag.
M Mehta
Ranch Hand

Joined: Aug 03, 2006
Posts: 92
HI Merrill,

Thanks for the reply again !! Its very informative for me.. Meanwhile I worked on your first advice (using a single form). I removed the form from both the JSP files and created a third JSP file with a single form. In that form I included the first two files and used <logic> tag for displaying/hiding the second one. This is working fine as now the same action is being used for both of them and both are submitted on submission of the form.

Thanks again for your help.
subject: Combining two Forms
Similar Threads
cancel button issue
object doesn't support this property or method
from submission
Commad Buttons and Command Links are not working for a single Click in icefaces.
Three forms in jsp page