开发者

JSF submit button - form is X times submited during one second (until view recreated) - how to prevent it?

开发者 https://www.devze.com 2023-04-01 07:48 出处:网络
I have a problem when I submit the form (pressing ENTER key or clicking mouse) - if I press it x times quickly, faster then actual view is recreated (e.g. ten times in one second), the form data are s

I have a problem when I submit the form (pressing ENTER key or clicking mouse) - if I press it x times quickly, faster then actual view is recreated (e.g. ten times in one second), the form data are sent ten times and ten times are saved to database.

And no matter if there is some validation, e.g. check that data can be saved only once.

Next submit, after view refresh, works normally. Data are not saved, because the validator is working now..

So, malevolent user fill the form and instead of submit it normally, he is quickly clicking on submit button until the view is rebuilded and I have X records in database instead of one :-)

How can I prevent this strange behaviour?

H开发者_C百科ow to forbid to press submit button until view is rebuild?


You may want to add something like a DoubleClickFilter to your web.xml. Another way is to work with Tokens which you validate each request and become invalid after first request so that subsequent requests fail your validation.


SOLUTION:

//Initialize our submit flag to false
var formSubmitted = false;

/**
 * Prevent from submitting a form more than once
 * @returns {Boolean}
 */
function submitForm()
{   
  //has the form been submitted before?
  if( formSubmitted == true )
  {
     alert("This form has already been submitted!");
     return false;
  }
  formSubmitted = true;

  return true; // Submit form
}

and the submit button:

<h:commandButton ... action="#{bean.action}" onclick="return submitForm();"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号