重新启用 p:commandButton 不触发 ajax [英] Re-enabled p:commandButton not firing ajax
问题描述
如果我启动一个禁用的 commandButton,即使在重新启用按钮后,AJAX 事件也不会触发.
If I initiate a commandButton disabled the AJAX event does not fire even after re-enabling the button.
<p:commandButton id="btnAJAX" value="AJAX" widgetVar="btnAJAX" disabled="true" action="#{bean.neverReached()}"/>
<p:commandButton id="btnEnabler" value="Enable" oncomplete="btnAJAX.enable()"/>
这里发现了类似的问题:http://forum.primefaces.org/viewtopic.php?f=3&t=7817
Similar problem identitifed here : http://forum.primefaces.org/viewtopic.php?f=3&t=7817
我使用的是 primefaces 3.0.1 和 JDK 1.7
I am using primefaces 3.0.1 and JDK 1.7
有什么解决办法吗?
推荐答案
您需要通过 JSF 启用按钮,而不是通过 JavaScript/HTML DOM.在表单提交的处理过程中,JSF 还将在服务器端视图状态中验证按钮是否启用,作为防止篡改请求的一部分.
You need to enable the button by JSF, not by JavaScript/HTML DOM. During processing of the form submit, JSF will verify in the server side view state as well if the button is enabled or not, as part of safeguard against tampered requests.
例如
<p:commandButton id="btnAJAX" value="AJAX" action="#{bean.someAction}" disabled="#{!bean.enabled}" />
<p:commandButton id="btnEnabler" value="Enable" action="#{bean.enableButton}" process="@this" update="btnAJAX" />
与
private boolean enabled;
public void enableButton() {
enabled = true;
}
public boolean isEnabled() {
return enabled;
}
确保 bean 至少是 @ViewScoped
而不是 @RequestScoped
,否则按钮的动作仍然会失败,因为在表单提交请求期间重新创建了 bean因此 enabled
属性将成为默认值,即 false
.
Make sure that the bean is at least @ViewScoped
and not @RequestScoped
, otherwise the action of button will still fail because the bean is recreated during the form submit request and thus the enabled
property will become the default value, which is false
.
这篇关于重新启用 p:commandButton 不触发 ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!