In JSF2, is there any way to handle the multiple form submit (user clicks submit button multiple times)? Now we are getting the below exception in server
java.lang.IllegalStateException: Response already committed
java.lang.IllegalStateException: Response already committed
javax.faces.FacesException: socket write error: Connection aborted by peer
That depends on how you’re submitting the form. If you’re using
<f:ajax>or any other Ajax based tag to perform the submit by Ajax, then your best bet is to usejsf.ajax.addOnEvent()to add a function which disables the button when the Ajax request is about to be sent and re-enables it after the Ajax response is retrieved.E.g. as follows, which you include after the JSF Ajax scripts are included, e.g. inside a
<script>or<h:outputScript>referring a.jsfile in<h:head>.If you’re not using Ajax to perform the submit, then one of the ways is to throw in a
setTimeout()function inonclickwhich disables the button a few ms after click.Basically,
The
setTimeout()is mandatory, because when you disable it immediately, then thename=valuepair of the button won’t be sent along with the request which would cause JSF to be unable to identify theactionto be executed. You can of course refactor it to some DOM ready or window onload function which applies this for all submit buttons (jQuery would be tremendously helpful in this).