重新启用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
对此有什么解决办法吗?
Is there any solution to this?
推荐答案
您需要通过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屋!