views:

2566

answers:

6

Hello guys,

I`m developing an application using Spring WebFlow 2, Facelets and JSF. One of my flows does have a page that must trigger a form submit at certain events. For each different action, a different view must be presented. So, I'm trying to activate the following javascript code to perform the submission:

function myFormSubmit( eventId ) {
   var formAction = document.myForm.action;
   document.myForm.action = formAction + '&_eventId=' + eventId;
   document.myForm.submit();
}

Unfortunatelly, this doesn't triggers the requested transition in my flow. The page doesn't change. Does anyone knows how to deal with this?

Thanks, Alexandre

A: 

Are you looking for this parameter in your GET params or your POST params? It won't appear in the POST.

Update:

ok,so to illustrate, 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 (non-IE)* browser and see if it works. If so, the GET vs. POST param is the issue.

* non-IE: trying to set the name or type in IE will FAIL, thus don't test there.
scunliffe
I`m not sure if I understood your question, but what I`m trying to do is to add the parameter '_eventId' to my POST request. Debuging the request with firebug, it does add the parameter, but it`s not being processed as I`ve thought by the server
Alexandre
I added some sample code for you to test with.
scunliffe
+1  A: 

Don't know anything about SringFaceletsSF, but I think that event handler should probably return true. Otherwise your browser will not submit the form.

so:

function myFormSubmit( eventId ) {
   var formAction = document.myForm.action;
   document.myForm.action = formAction + '&_eventId=' + eventId;
   document.myForm.submit();
   return true;
}
Triptych
Debuging with firebug I can see that the submit is done. Anyway, I`ve added 'return true' as you've said, and the result is the same
Alexandre
+1  A: 

On closer inspection, there are a bunch of problems with this code.

document.myForm.action is a non-standard way of getting elements. The better, cross-browser way is:

document.getElementById('myFormId');

Blindly appending an ampersand to the form action URL assumes that there is already another url parameter, and so there is already a question mark after the URL. If this is not the case the following line breaks:

document.myForm.action = formAction + '&_eventId=' + eventId;

Lastly, if you are calling this function as an onsubmit handler, I believe you should just return true, and not call the myForm.submit() method

Triptych
Thanks for the tips Triptych. I'm aware of the problems you pointed. For now, I'm just trying to make it work in Firefox. That's the excuse for such bad code. There is indeed another url parameter, so the resulting action url is OK.
Alexandre
A: 

Modifying form.action is a very poor way of adding information to a form submission. I suggest putting a hidden input in your form that you can populate with your eventID.

chaos
+1  A: 

I've got the solution in the Spring WebFlow forum. According to Jeremy Grelle, the submission of "_eventId" parameter does not trigger transitions when integrating Spring WebFlow to JSF. The solution he gave was to create a JSF PhaseListener that creates a JSF action when an "_eventId" parameter is sent.

The phaseListener code can be seen at http://forum.springsource.org/showthread.php?p=219358#post219358.

Alexandre
A: 

Is there any reason you can't use the Spring Faces components which come with SWF? There's an ajaxEvent tag you can use to wrap elements. It activates on given javascript events (onclick, onchange) and invokes an action of your choice.

InverseFalcon