2012-01-31 218 views
2

该场景如下所示:如何启用/禁用JSF命令按钮

屏幕上有两个按钮,比如button1和button2。在页面加载时,button2被禁用。当用户点击按钮1时,按钮2将被启用。我使用javascript window.onload事件和button1上的onclick事件实现了此功能。我的问题是,当用户点击button1时,它会触发页面重新加载。据说,在页面重新载入后,由于onload事件,button2被禁用。

是否有另一种方法来实现此功能?

顺便说一句,我使用的是primefaces。

+2

向我们显示您的代码。 – Eduard 2012-01-31 13:07:10

+0

听起来像是你可以使用一个答案从这里http://stackoverflow.com/questions/9000669/re-enabled-pcommandbutton-not-firing-ajax – mrembisz 2012-01-31 18:52:58

回答

11

primeface页是这样的,

<p:commandButton update="panel1" actionListener="#{bean.button1}" value="button1" disabled="#{bean.disable}"> 
    <f:setPropertyActionListener value="#{false}" target="#{bean.disable}"/> 
</p:commandButton> 


<p:commandButton update="panel2" actionListener="#{bean.button2}" value="button1" disabled="#{!(bean.disable)}"> 
    <f:setPropertyActionListener value="#{true}" target="#{bean.disable}"/>  
</p:commandButton> 

管理豆:

public class Bean { 

    private boolean disable; 

    // default constructor 
    public Bean(){ 
     this.disable= false; 
    } 

    public boolean isDisable() { 
     return disable; 
    } 
    public void setDisable(boolean disable) { 
     this.disable = disable; 
    } 
} 
+0

你为什么要叫'actionListener'? – alexander 2015-08-16 11:44:29

+0

@Kushan:你能解释一下,你为什么使用'actionListener'? – alexander 2015-08-25 05:53:55

0

如果使用的是JSF 2.0,你可以使用f:ajax标签做到这一点。

您可以在豆

disabled="#{bean.isDisabled}" 

禁用与条件的按钮,当你重新呈现,当你点击按钮1按钮,该按钮可以启用。

1

有一个在我看来有很多简单的方法。

只需创建一个managed bean一个boolean属性(吸气剂+ setter方法)与javax.faces.bean.ViewScopedjavax.faces.bean.ManagedBean注解。

您可以通过此属性(button2State)处理禁用状态。

<h:form> 
    <p:commandButton id="button1" update="button2" 
     action="#{bean.setButton2State(false)}" 
     value="Enable second button" /> 
    <p:commandButton id="button2" disabled="#{bean.button2State}" 
     value="button disabled one page load" /> 
</h:form> 

的第一个按钮调用属性的setter,并设置button2Statefalse启用按钮。这个例子也使用ajax来只更新按钮。