Javascript Form Submission for Activating Spring Web Flow Transitions with JSF Integration
I am developing an application using Spring WebFlow 2, Facelets and JSF. One of my threads has a page that should trigger form submission on certain events. For each other action, a different view must be presented. So I am trying to activate the following javascript code to execute the view:
function myFormSubmit( eventId ) {
var formAction = document.myForm.action;
document.myForm.action = formAction + '&_eventId=' + eventId;
document.myForm.submit();
}
Unfortunately this does not trigger the requested transition on my thread. The page does not change. Does anyone know how to deal with this?
Thank you, Alexander
source to share
I have a solution on the Spring WebFlow forum. According to Jeremy Grelle, representing the "_eventId" parameter does not trigger transitions when integrating Spring WebFlow into JSF. The solution he gave was to create a JSF PhaseListener that creates a JSF action when the "_eventId" parameter is sent.
The PhaseListener code can be seen at http://forum.springsource.org/showthread.php?p=219358#post219358 .
source to share
Don't know anything about SringFaceletsSF, but I think the event handler should return true
. Otherwise, your browser won't submit the form.
So:
function myFormSubmit( eventId ) {
var formAction = document.myForm.action;
document.myForm.action = formAction + '&_eventId=' + eventId;
document.myForm.submit();
return true;
}
source to share
Upon closer inspection, there are a number of issues with this code.
document.myForm.action is a non-standard way to get elements. Better, cross-browser way:
document.getElementById('myFormId');
Blindly adding an ampersand to the form action url assumes there is another url parameter and there is already a question mark after the url. If it is not, the next line breaks:
document.myForm.action = formAction + '&_eventId=' + eventId;
Finally, if you call this function as an onsubmit handler, I believe you should just return true and not call the method myForm.submit()
source to share
Are you looking for this parameter in GET parameters or POST parameters? It will not appear in POST.
Update:
ok so for illustration try this ...
change your function to do this:
function myFormSubmit( eventId ) {
var formAction = document.myForm.action;
//document.myForm.action = formAction + '&_eventId=' + eventId;
var myAction = document.createElement('input');
myAction.setAttribute('name', '_eventId');
myAction.setAttribute('type', 'hidden');
myAction.setAttribute('value', eventId);
document.myForm.appendChild(myAction);
document.myForm.submit();
}
Test in any browser (not IE) * and see if it works. If so, the problem is GET vs. POST is a problem.
* non-IE: trying to set the name or type in IE will FAIL, thus don't test there.
source to share