Advice on using Javascript "this.form.submit" with a JSF form

Hello all,

I'm sure that someone will point out that this is already covered somewhere... but I posted a problem I was having at

http://forum.java.sun.com/thread.jspa?threadID=5140905&tstart=15

and no one answered, so even if it is covered, it doesn't hurt to say it again.

The issue is this: sometimes we want to design forms that submit themselves in response to certain events, as in "onchange" or "onclick" etc. To these events, we add a bit of Javascript that says something like "this.form.submit()".

I ran into a problem recently where this was not working for me: the form would be submitted and go through validation BUT it would not invoke the specified action (a method in a backing bean as is typical).

Reason for the problem: pressing the submit *button" will send along with the form a parameter that represents the button. But using Javascript to send off a submit() will *not* include that parameter - and JSF needs that parameter it seems to invoke the backing bean; if the parameter is there, it will invoke the method, otherwise no. Mind you, the entire JSF lifecycle will occur, even going into Invoke Application - but it just won't be invoked.

Solution: if you want the submission to really occur, and invoke the backing bean method, before doing a this.form.submit, add a line where you alter the action to include a bogus submit parameter as part of the query string:

this.form.action = this.form.action + "?formSubmit=Value"

If this helps anyone, great. Took me a couple of days to figure out something that is probably self-evident to many of you. Oh well....

-A

Message was edited by:

bland999

[1707 byte] By [bland999a] at [2007-11-26 19:42:58]
# 1
Rather dodocument.getElementById('formId:commandButtonId').click();// ordocument.getElementById('formId:commandLinkId').onclick();
BalusCa at 2007-7-9 22:25:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Excellent! I guess it's time to finally read that Javascript Bible.
bland999a at 2007-7-9 22:25:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Also read the DOM bible ;)
BalusCa at 2007-7-9 22:25:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Thanks for sharing your work. You just saved me a day's work or more :)
cafemondegara at 2007-7-9 22:25:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...