You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Michael Heinen <mh...@googlemail.com> on 2012/02/02 11:05:44 UTC

onsumit not exeucted for ajax requests - alternative?

Hi,

I am currently migrating an application to JSF 2.1

I have a lot of ajax commands (richfaces) and some new f:ajax tags.
My forms contain some js functions which should be called during 
onsubmit but unfortunately onsubmit is not called for ajax requests.

How can I specify some JS functions that should be executed before any 
ajax call?
I do not want to add them to a few hundred commands manually and would 
like to define the calls on a few spots as possible.

Thanks,
Michael

Re: onsumit not exeucted for ajax requests - alternative?

Posted by Werner Punz <we...@gmail.com>.
Definitely, our queue mechanisms just implement the bare necessities, 
but advanced queuing like multiple queues are in the Richfaces Queues
to my knowledge, it might be the better choice.

Werner


Am 06.02.12 16:00, schrieb Michael Heinen:
> Werner, thanks for the explanation and pointing me to the Blog!
> I'll have a look any play around with this.
> Maybe RichFaces Queues are also an alternative for me because most of my
> ajax commands are from Richfaces.
>
> Thanks,
> Michael
>
>
> Am 06.02.2012 14:47, schrieb Werner Punz:
>> > You can use the global listeners however, set yourself a marker
>> class or
>> > an attribute in your form element which should trigger the listener
>> > and if you get an ajax request you can check for this marker whether
>> you
>> > should intercept with your code or not.
>>
>> Just forgot to mention in your case to prevent double submits,
>> throwing an error to cancel the request is perfectly valid.
>> For limited time double submit prevention this might be handy as well
>> http://www.irian.at/de/blog/-/blogs/apache-myfaces-queue-control
>>
>> Werner
>>
>>
>> Am 06.02.12 14:42, schrieb Werner Punz:
>>> The onsubmit is a simple callback coming from the dom tree, it is not
>>> executed during ajax because there is no form submit performed.
>>>
>>> Have in mind on some browsers you dont even get the event with special
>>> dom configurations (aka, simple form posts not by submit buttons etc...)
>>> or the event cannot be blocked, so do not entirely rely on this event,
>>> it is flakey.
>>>
>>> For the ajax case:
>>>
>>> You can use the global listeners however, set yourself a marker class or
>>> an attribute in your form element which should trigger the listener
>>> and if you get an ajax request you can check for this marker whether you
>>> should intercept with your code or not.
>>>
>>> Here is a short pseudo code example:
>>> function submitHandler(data) {
>>> if(data.status == "begin") {
>>> var src = data.source;
>>>
>>> var form = getParentForm(src);
>>> if(hasClass(form, "myMarker")) {
>>> intercept();
>>> }
>>> }
>>> }
>>>
>>>
>>> You cannot cancel the request that way, but at least you have a
>>> callback. Practically by throwing an error in your code the request then
>>> will be cancelled and an error handler will be called.
>>> That might be a dirty way to cancel the request upfront.
>>>
>>>
>>>
>>>
>>> Am 06.02.12 14:12, schrieb Michael Heinen:
>>>> Thanks Werner for the suggestion.
>>>>
>>>> I created meanwhile a JIRA issue for this:
>>>> https://issues.apache.org/jira/browse/MYFACES-3460
>>>>
>>>> A global js listener is unfortunately not what I need.
>>>> In my case it depends on the form, whether a js should be executed
>>>> during submission or not.
>>>> Some requests can run in parallel (e.g. autocompletion, pulls) while
>>>> others are blocking.
>>>>
>>>> Do you know why the simple onsubmit of a form is not executed for ajax
>>>> requests?
>>>>
>>>> Michael
>>>>
>>>> Am 06.02.2012 14:02, schrieb Werner Punz:
>>>>> Hi if you need to execute commands before any arbitrary ajax call
>>>>> then you can add your own global ajax request listener via
>>>>> jsf.ajax.addOnEvent(callback)
>>>>>
>>>>>
>>>>> The callback is called three times per request, same as if you would
>>>>> set it directly within the request, but on a global scale.
>>>>>
>>>>> http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/js-api/symbols/jsf.ajax.html
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Unfortunately a deregistration is not possible within the jsf api
>>>>> within myfaces there is a way but that would break the compatibility
>>>>> of the code with Mojarra.
>>>>>
>>>>>
>>>>> Werner
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Am 02.02.12 11:05, schrieb Michael Heinen:
>>>>>> Hi,
>>>>>>
>>>>>> I am currently migrating an application to JSF 2.1
>>>>>>
>>>>>> I have a lot of ajax commands (richfaces) and some new f:ajax tags.
>>>>>> My forms contain some js functions which should be called during
>>>>>> onsubmit but unfortunately onsubmit is not called for ajax requests.
>>>>>>
>>>>>> How can I specify some JS functions that should be executed before
>>>>>> any
>>>>>> ajax call?
>>>>>> I do not want to add them to a few hundred commands manually and
>>>>>> would
>>>>>> like to define the calls on a few spots as possible.
>>>>>>
>>>>>> Thanks,
>>>>>> Michael
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>>
>>
>>
>>
>
>



Re: onsumit not exeucted for ajax requests - alternative?

Posted by Michael Heinen <mh...@googlemail.com>.
Werner, thanks for the explanation and pointing me to the Blog!
I'll have a look any play around with this.
Maybe RichFaces Queues are also an alternative for me because most of my 
ajax commands are from Richfaces.

Thanks,
Michael


Am 06.02.2012 14:47, schrieb Werner Punz:
> > You can use the global listeners however, set yourself a marker 
> class or
> > an attribute in your form element which should trigger the listener
> > and if you get an ajax request you can check for this marker whether 
> you
> > should intercept with your code or not.
>
> Just forgot to mention in your case to prevent double submits, 
> throwing an error to cancel the request is perfectly valid.
> For limited time double submit prevention this might be handy as well
> http://www.irian.at/de/blog/-/blogs/apache-myfaces-queue-control
>
> Werner
>
>
> Am 06.02.12 14:42, schrieb Werner Punz:
>> The onsubmit is a simple callback coming from the dom tree, it is not
>> executed during ajax because there is no form submit performed.
>>
>> Have in mind on some browsers you dont even get the event with special
>> dom configurations (aka, simple form posts not by submit buttons etc...)
>> or the event cannot be blocked, so do not entirely rely on this event,
>> it is flakey.
>>
>> For the ajax case:
>>
>> You can use the global listeners however, set yourself a marker class or
>> an attribute in your form element which should trigger the listener
>> and if you get an ajax request you can check for this marker whether you
>> should intercept with your code or not.
>>
>> Here is a short pseudo code example:
>> function submitHandler(data) {
>> if(data.status == "begin") {
>> var src = data.source;
>>
>> var form = getParentForm(src);
>> if(hasClass(form, "myMarker")) {
>> intercept();
>> }
>> }
>> }
>>
>>
>> You cannot cancel the request that way, but at least you have a
>> callback. Practically by throwing an error in your code the request then
>> will be cancelled and an error handler will be called.
>> That might be a dirty way to cancel the request upfront.
>>
>>
>>
>>
>> Am 06.02.12 14:12, schrieb Michael Heinen:
>>> Thanks Werner for the suggestion.
>>>
>>> I created meanwhile a JIRA issue for this:
>>> https://issues.apache.org/jira/browse/MYFACES-3460
>>>
>>> A global js listener is unfortunately not what I need.
>>> In my case it depends on the form, whether a js should be executed
>>> during submission or not.
>>> Some requests can run in parallel (e.g. autocompletion, pulls) while
>>> others are blocking.
>>>
>>> Do you know why the simple onsubmit of a form is not executed for ajax
>>> requests?
>>>
>>> Michael
>>>
>>> Am 06.02.2012 14:02, schrieb Werner Punz:
>>>> Hi if you need to execute commands before any arbitrary ajax call
>>>> then you can add your own global ajax request listener via
>>>> jsf.ajax.addOnEvent(callback)
>>>>
>>>>
>>>> The callback is called three times per request, same as if you would
>>>> set it directly within the request, but on a global scale.
>>>>
>>>> http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/js-api/symbols/jsf.ajax.html 
>>>>
>>>>
>>>>
>>>>
>>>> Unfortunately a deregistration is not possible within the jsf api
>>>> within myfaces there is a way but that would break the compatibility
>>>> of the code with Mojarra.
>>>>
>>>>
>>>> Werner
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> Am 02.02.12 11:05, schrieb Michael Heinen:
>>>>> Hi,
>>>>>
>>>>> I am currently migrating an application to JSF 2.1
>>>>>
>>>>> I have a lot of ajax commands (richfaces) and some new f:ajax tags.
>>>>> My forms contain some js functions which should be called during
>>>>> onsubmit but unfortunately onsubmit is not called for ajax requests.
>>>>>
>>>>> How can I specify some JS functions that should be executed before 
>>>>> any
>>>>> ajax call?
>>>>> I do not want to add them to a few hundred commands manually and 
>>>>> would
>>>>> like to define the calls on a few spots as possible.
>>>>>
>>>>> Thanks,
>>>>> Michael
>>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>>
>
>
>


Re: onsumit not exeucted for ajax requests - alternative?

Posted by Werner Punz <we...@gmail.com>.
 > You can use the global listeners however, set yourself a marker class or
 > an attribute in your form element which should trigger the listener
 > and if you get an ajax request you can check for this marker whether you
 > should intercept with your code or not.

Just forgot to mention in your case to prevent double submits, throwing 
an error to cancel the request is perfectly valid.
For limited time double submit prevention this might be handy as well
http://www.irian.at/de/blog/-/blogs/apache-myfaces-queue-control

Werner


Am 06.02.12 14:42, schrieb Werner Punz:
> The onsubmit is a simple callback coming from the dom tree, it is not
> executed during ajax because there is no form submit performed.
>
> Have in mind on some browsers you dont even get the event with special
> dom configurations (aka, simple form posts not by submit buttons etc...)
> or the event cannot be blocked, so do not entirely rely on this event,
> it is flakey.
>
> For the ajax case:
>
> You can use the global listeners however, set yourself a marker class or
> an attribute in your form element which should trigger the listener
> and if you get an ajax request you can check for this marker whether you
> should intercept with your code or not.
>
> Here is a short pseudo code example:
> function submitHandler(data) {
> if(data.status == "begin") {
> var src = data.source;
>
> var form = getParentForm(src);
> if(hasClass(form, "myMarker")) {
> intercept();
> }
> }
> }
>
>
> You cannot cancel the request that way, but at least you have a
> callback. Practically by throwing an error in your code the request then
> will be cancelled and an error handler will be called.
> That might be a dirty way to cancel the request upfront.
>
>
>
>
> Am 06.02.12 14:12, schrieb Michael Heinen:
>> Thanks Werner for the suggestion.
>>
>> I created meanwhile a JIRA issue for this:
>> https://issues.apache.org/jira/browse/MYFACES-3460
>>
>> A global js listener is unfortunately not what I need.
>> In my case it depends on the form, whether a js should be executed
>> during submission or not.
>> Some requests can run in parallel (e.g. autocompletion, pulls) while
>> others are blocking.
>>
>> Do you know why the simple onsubmit of a form is not executed for ajax
>> requests?
>>
>> Michael
>>
>> Am 06.02.2012 14:02, schrieb Werner Punz:
>>> Hi if you need to execute commands before any arbitrary ajax call
>>> then you can add your own global ajax request listener via
>>> jsf.ajax.addOnEvent(callback)
>>>
>>>
>>> The callback is called three times per request, same as if you would
>>> set it directly within the request, but on a global scale.
>>>
>>> http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/js-api/symbols/jsf.ajax.html
>>>
>>>
>>>
>>> Unfortunately a deregistration is not possible within the jsf api
>>> within myfaces there is a way but that would break the compatibility
>>> of the code with Mojarra.
>>>
>>>
>>> Werner
>>>
>>>
>>>
>>>
>>>
>>> Am 02.02.12 11:05, schrieb Michael Heinen:
>>>> Hi,
>>>>
>>>> I am currently migrating an application to JSF 2.1
>>>>
>>>> I have a lot of ajax commands (richfaces) and some new f:ajax tags.
>>>> My forms contain some js functions which should be called during
>>>> onsubmit but unfortunately onsubmit is not called for ajax requests.
>>>>
>>>> How can I specify some JS functions that should be executed before any
>>>> ajax call?
>>>> I do not want to add them to a few hundred commands manually and would
>>>> like to define the calls on a few spots as possible.
>>>>
>>>> Thanks,
>>>> Michael
>>>>
>>>
>>>
>>>
>>
>>
>
>
>



Re: onsumit not exeucted for ajax requests - alternative?

Posted by Werner Punz <we...@gmail.com>.
The onsubmit is a simple callback coming from the dom tree, it is not 
executed during ajax because there is no form submit performed.

Have in mind on some browsers you dont even get the event with special 
dom configurations (aka, simple form posts not by submit buttons etc...)
or the event cannot be blocked, so do not entirely rely on this event, 
it is flakey.

For the ajax case:

You can use the global listeners however, set yourself a marker class or 
an attribute in your form element which should trigger the listener
and if you get an ajax request you can check for this marker whether you 
should intercept with your code or not.

Here is a short pseudo code example:
function submitHandler(data) {
	if(data.status == "begin") {
		var src = data.source;
		
		var form = getParentForm(src);
		if(hasClass(form, "myMarker")) {
			intercept();
		}
         }
}


You cannot cancel the request that way, but at least you have a 
callback. Practically by throwing an error in your code the request then 
will be cancelled and an error handler will be called.
That might be a dirty way to cancel the request upfront.




Am 06.02.12 14:12, schrieb Michael Heinen:
> Thanks Werner for the suggestion.
>
> I created meanwhile a JIRA issue for this:
> https://issues.apache.org/jira/browse/MYFACES-3460
>
> A global js listener is unfortunately not what I need.
> In my case it depends on the form, whether a js should be executed
> during submission or not.
> Some requests can run in parallel (e.g. autocompletion, pulls) while
> others are blocking.
>
> Do you know why the simple onsubmit of a form is not executed for ajax
> requests?
>
> Michael
>
> Am 06.02.2012 14:02, schrieb Werner Punz:
>> Hi if you need to execute commands before any arbitrary ajax call
>> then you can add your own global ajax request listener via
>> jsf.ajax.addOnEvent(callback)
>>
>>
>> The callback is called three times per request, same as if you would
>> set it directly within the request, but on a global scale.
>>
>> http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/js-api/symbols/jsf.ajax.html
>>
>>
>> Unfortunately a deregistration is not possible within the jsf api
>> within myfaces there is a way but that would break the compatibility
>> of the code with Mojarra.
>>
>>
>> Werner
>>
>>
>>
>>
>>
>> Am 02.02.12 11:05, schrieb Michael Heinen:
>>> Hi,
>>>
>>> I am currently migrating an application to JSF 2.1
>>>
>>> I have a lot of ajax commands (richfaces) and some new f:ajax tags.
>>> My forms contain some js functions which should be called during
>>> onsubmit but unfortunately onsubmit is not called for ajax requests.
>>>
>>> How can I specify some JS functions that should be executed before any
>>> ajax call?
>>> I do not want to add them to a few hundred commands manually and would
>>> like to define the calls on a few spots as possible.
>>>
>>> Thanks,
>>> Michael
>>>
>>
>>
>>
>
>



Re: onsumit not exeucted for ajax requests - alternative?

Posted by Michael Heinen <mh...@googlemail.com>.
Thanks Werner for the suggestion.

I created meanwhile a JIRA issue for this: 
https://issues.apache.org/jira/browse/MYFACES-3460

A global js listener is unfortunately not what I need.
In my case it depends on the form, whether a js should be executed 
during submission or not.
Some requests can run in parallel (e.g. autocompletion, pulls) while 
others are blocking.

Do you know why the simple onsubmit of a form is not executed for ajax 
requests?

Michael

Am 06.02.2012 14:02, schrieb Werner Punz:
> Hi if you need to execute commands before any arbitrary ajax call
> then you can add your own global ajax request listener via
> jsf.ajax.addOnEvent(callback)
>
>
> The callback is called three times per request, same as if you would
> set it directly within the request, but on a global scale.
>
> http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/js-api/symbols/jsf.ajax.html 
>
>
> Unfortunately a deregistration is not possible within the jsf api 
> within myfaces there is a way but that would break the compatibility 
> of the code with Mojarra.
>
>
> Werner
>
>
>
>
>
> Am 02.02.12 11:05, schrieb Michael Heinen:
>> Hi,
>>
>> I am currently migrating an application to JSF 2.1
>>
>> I have a lot of ajax commands (richfaces) and some new f:ajax tags.
>> My forms contain some js functions which should be called during
>> onsubmit but unfortunately onsubmit is not called for ajax requests.
>>
>> How can I specify some JS functions that should be executed before any
>> ajax call?
>> I do not want to add them to a few hundred commands manually and would
>> like to define the calls on a few spots as possible.
>>
>> Thanks,
>> Michael
>>
>
>
>


Re: onsumit not exeucted for ajax requests - alternative?

Posted by Werner Punz <we...@gmail.com>.
Hi if you need to execute commands before any arbitrary ajax call
then you can add your own global ajax request listener via
jsf.ajax.addOnEvent(callback)


The callback is called three times per request, same as if you would
set it directly within the request, but on a global scale.

http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/js-api/symbols/jsf.ajax.html

Unfortunately a deregistration is not possible within the jsf api within 
myfaces there is a way but that would break the compatibility of the 
code with Mojarra.


Werner





Am 02.02.12 11:05, schrieb Michael Heinen:
> Hi,
>
> I am currently migrating an application to JSF 2.1
>
> I have a lot of ajax commands (richfaces) and some new f:ajax tags.
> My forms contain some js functions which should be called during
> onsubmit but unfortunately onsubmit is not called for ajax requests.
>
> How can I specify some JS functions that should be executed before any
> ajax call?
> I do not want to add them to a few hundred commands manually and would
> like to define the calls on a few spots as possible.
>
> Thanks,
> Michael
>



Re: onsumit not exeucted for ajax requests - alternative?

Posted by Michael Heinen <mh...@googlemail.com>.
Hi,

onsubmit of the form is executed for the standard Command Button but not 
for an Ajax Button.
I tried with myFaces 2.1.5, 2.0.11 and with mojarra 2.1.6 and 2.0.8.

Here is a very simple sample:
<html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:f="http://java.sun.com/jsf/core">
<ui:composition>

<h:form id="myform" onsubmit="alert('submitted')" >
<h:outputText id="oldCounter" value="oldCounter: 
#{MyController.counter}"/><br/>
<h:outputText id="newCounter" value="newCounter: 
#{MyController.counter}"/><br/>

<h:commandButton value="AjaxButton" 
actionListener="#{MyController.increase}" >
<f:ajax render="newCounter" execute="@this"/>
</h:commandButton><br/>
<h:commandButton value="Button" actionListener="#{MyController.increase}"/>
</h:form>

</ui:composition>
</html>

@ManagedBean(name = "MyController")
@SessionScoped
public class MyController{
   private int counter = 1;
   public int getCounter() {
     return counter;
   }
   public void increase(ActionEvent ae) {
     counter++;
   }
}

Is it a bug and should I add it to JIRA?
Is there any workaround to execute the js of the outer form for ajax 
commands?
It was working well with JSF 1.2 and richfaces 3.3.3, but now I'm stuck

Michael


Am 02.02.2012 12:09, schrieb Milo van der Zee:
> Hello Michael,
>
> you are completely right. I have the onbegin defined on every action
> initiating component but that is precisely what you don't want. That
> would result in very much changes...
>
> <a4j:status id="status" startText="" stopText=""
> onstart="showAjaxActive();" onstop="hideAjaxActive();"
> onerror="handleError(event);"/>
>
> This does handle every ajax call but then it has nothing to do with form
> submissions.
>
> I'm actually surprised that onsubmit does not work. Isn't it just a bug?
>
> MAG,
> Milo
>
> On Thu, 2012-02-02 at 11:35 +0100, Michael Heinen wrote:
>> Hi Milo,
>>
>> what's "onbegin" ?  Where do i define that?
>>
>> This attribute is not specifed for h:form.
>> See http://myfaces.apache.org/core21/myfaces-impl/tagdoc/h_form.html
>>
>> Michael
>>
>> Am 02.02.2012 11:21, schrieb Milo van der Zee:
>>> Hello Michael,
>>>
>>> might be that 'onbegin' does what you want?
>>>
>>> MAG,
>>> Milo van der Zee
>>>
>>> On Thu, 2012-02-02 at 11:05 +0100, Michael Heinen wrote:
>>>> Hi,
>>>>
>>>> I am currently migrating an application to JSF 2.1
>>>>
>>>> I have a lot of ajax commands (richfaces) and some new f:ajax tags.
>>>> My forms contain some js functions which should be called during
>>>> onsubmit but unfortunately onsubmit is not called for ajax requests.
>>>>
>>>> How can I specify some JS functions that should be executed before any
>>>> ajax call?
>>>> I do not want to add them to a few hundred commands manually and would
>>>> like to define the calls on a few spots as possible.
>>>>
>>>> Thanks,
>>>> Michael
>>>>
>>>
>>
>
>


Re: onsumit not exeucted for ajax requests - alternative?

Posted by Michael Heinen <mh...@googlemail.com>.
Hi Milo,

what's "onbegin" ?  Where do i define that?

This attribute is not specifed for h:form.
See http://myfaces.apache.org/core21/myfaces-impl/tagdoc/h_form.html

Michael

Am 02.02.2012 11:21, schrieb Milo van der Zee:
> Hello Michael,
>
> might be that 'onbegin' does what you want?
>
> MAG,
> Milo van der Zee
>
> On Thu, 2012-02-02 at 11:05 +0100, Michael Heinen wrote:
>> Hi,
>>
>> I am currently migrating an application to JSF 2.1
>>
>> I have a lot of ajax commands (richfaces) and some new f:ajax tags.
>> My forms contain some js functions which should be called during
>> onsubmit but unfortunately onsubmit is not called for ajax requests.
>>
>> How can I specify some JS functions that should be executed before any
>> ajax call?
>> I do not want to add them to a few hundred commands manually and would
>> like to define the calls on a few spots as possible.
>>
>> Thanks,
>> Michael
>>
>
>


Re: onsumit not exeucted for ajax requests - alternative?

Posted by Milo van der Zee <mi...@vanderzee.org>.
Hello Michael,

might be that 'onbegin' does what you want?

MAG,
Milo van der Zee

On Thu, 2012-02-02 at 11:05 +0100, Michael Heinen wrote:
> Hi,
> 
> I am currently migrating an application to JSF 2.1
> 
> I have a lot of ajax commands (richfaces) and some new f:ajax tags.
> My forms contain some js functions which should be called during 
> onsubmit but unfortunately onsubmit is not called for ajax requests.
> 
> How can I specify some JS functions that should be executed before any 
> ajax call?
> I do not want to add them to a few hundred commands manually and would 
> like to define the calls on a few spots as possible.
> 
> Thanks,
> Michael
>