Action of JSF h:commandButton not invoked after setting disabled=true in JavaScript

我与影子孤独终老i 提交于 2020-01-11 11:55:08

问题


Who can explain the following behaviour? When disabling a command button in JavaScript, the action method is never executed.

<h:form>
    <h:commandButton value="test" action="#{bean.test}" onclick="disabled=true" />
</h:form>

回答1:


When a HTML form element is disabled, then its name=value pair won't be sent as HTTP request parameter and hence JSF will never find it in the request parameter map. For command buttons this in turn means that the action is never invoked.

You need to disable the button shortly after the form has been submitted. The proposed suggestions to use disabled attribute make no sense as it's too late. It's not set during submitting the form, but it's only set when the response returns.

To achieve your particular functional requirement in this particular case, you could use JS to timeout the disablement.

<h:commandButton ... onclick="setTimeout('document.getElementById(\'' + this.id + '\').disabled=true;', 50);" />

Much better is however to use <f:ajax onevent>

See also:

  • Pure Java/JSF implementation for double submit prevention
  • Reload parts of a page before action is executed?



回答2:


Try to disable the button from the BackBean using the property "disabled" of the component

disabled="#{!(bean.disable)}"

This option will work when the component is rendered, after the information sent



来源:https://stackoverflow.com/questions/12746137/action-of-jsf-hcommandbutton-not-invoked-after-setting-disabled-true-in-javascr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!