This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Struts and the fly likes Submitting a struts form outside of the form body Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Submitting a struts form outside of the form body" Watch "Submitting a struts form outside of the form body" New topic
Author

Submitting a struts form outside of the form body

Marcus Bowden
Greenhorn

Joined: Jul 22, 2009
Posts: 8
Hey guys,

I'm brand new here and fairly new to struts and jsp so go easy on me

I have a form that looks like this:



I want to be able to fire off one of the submits somewhere else in the page that is not inside this form. For example:



The above example doesn't work for me and using firebug console i get the error: "document.myform.submit() is not a function" //replacing myform with the actual form name

How do i do this? Any insight would be greatly appreciated and please forgive my rustyness with html/javascript/dom stuff too.

Cheers,

Marcus

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

Are you using the name 'submit' for any element in the form? I assume that's what the property="submit" does? (I wouldn't know, I'm not a Struts fan.)

If so, you have essentially wiped out the submit function.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Marcus Bowden
Greenhorn

Joined: Jul 22, 2009
Posts: 8
Bear Bibeault wrote:Are you using the name 'submit' for any element in the form? I assume that's what the property="submit" does? (I wouldn't know, I'm not a Struts fan.)

If so, you have essentially wiped out the submit function.


My only elements in this form that might wipe out the submit function are:



Ill change em up and give that a try.
Marcus Bowden
Greenhorn

Joined: Jul 22, 2009
Posts: 8
<html:submit property="foo"> just renders and html <input> element with type=submit. Having two of them could be problematic i guess.

The property attribute is the name of the request parameter that will be included with this submission, set to the specified value.

These definitions are from the struts framework page and its kind of confusing to me but the two <html:submit...>'s i have a functional at present doing their own functions and changing their property from submit breaks them.

Is there anyway to call these from outside the form?
Roberto Hernandez
Ranch Hand

Joined: Apr 29, 2009
Posts: 33
You would have to invoke a javascriipt function to submit your form


then in your button, declare an onclick attribute:



make sure you give your form a "Name" and "Id" that way you can get the form element
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

Roberto Hernandez wrote:You would have to invoke a javascriipt function to submit your form

Won't work. The point is that by naming a form element "submit", the JavaScript submit() function is blown away! See next post...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

If Struts forces you to name form elements "submit" then Struts is seriously broken as "submit" is a reserved name. Using it replaces the submit() function with the named element, rendering it impossible to call the function.

You should check into that enforced naming as I'm having a hard time believing that, much as I loathe Struts, it could be so short-sighted.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

If it turns out that Struts really does force you to use invalid names, there's a completely barf-able work-around:

After the <form> tag but before the offending elements:

This creates an alias to the submit() function before it gets blown away. You can then submit the form by calling the aliased function.

If that doesn't make you lose your lunch, nothing will.
Marcus Bowden
Greenhorn

Joined: Jul 22, 2009
Posts: 8
Just to clairify, my struts-config.xml file does allow for multiple submits in a form. Each one's value is what determines the next mapping. It's really more of how i can achieve that same functionality but just outside of the form.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

It has nothing to do with having multiple submit elements, and everything to do with the fact that any (one, two, or seventeen) elements have a name attribute of "submit".

Have you looked at the HTML being generated by Struts? Does any element have a name="submit" attribute? If so, you need to figure out how to have that not happen, or resort to the completely disgusting and ridiculous work-around that I posted.
Marcus Bowden
Greenhorn

Joined: Jul 22, 2009
Posts: 8
Ahh i see.

Well thanks for the insight Bear, much appreciated. I'm going to try a few things and ask the original writers if they have some ideas.

Cheers
Marcus Bowden
Greenhorn

Joined: Jul 22, 2009
Posts: 8
I broke down and put a the buttons in a div (absoloutly positioned to where i needed)

Its a bit better than aliasing the submit type...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Submitting a struts form outside of the form body