1

I am trying to make a commandButton enabled/disabled by using a checkbox. The commandbutton is disabled initially. When user checks the checkbox, the commandbutton turns into enabled. But it does not response when clicked the button.

If I make the commandbutton independent from checkbox, it works fine. But with checkbox, I get the problem that I mentioned above. Please help me

Here are codes.

index.xhtml

<h:form>        
  <h:selectBooleanCheckbox value="#{formSettings.licenseAccepted}" id="cb">
    <f:ajax event="click" render="suB cb"/>
  </h:selectBooleanCheckbox>
  <h:outputText value="#{formSettings.msg}"/><br/>

  <h:commandButton id="suB" disabled="false" value="Save" action="loginSuccessfull"/>
</h:form>

FormSettings.java

package classes;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class FormSettings 
{
    private boolean licenseAccepted = false;
    private String msg = "Accept License";

    public FormSettings(){};

    public boolean isLicenseAccepted(){return this.licenseAccepted;};
    public void setLicenseAccepted(boolean licenseAccepted){this.licenseAccepted = licenseAccepted;};
    public String getMsg(){return this.msg;};
    public void setMsg(String msg){this.msg = msg;};
}

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
    <navigation-rule>
        <from-view-id>/index.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>loginSuccessfull</from-outcome>
            <to-view-id>/login.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>

</faces-config>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This answer may help you.. [click here to know reasons for command button not getting invoked](http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked) – Karthikeyan Jul 10 '12 at 12:28

1 Answers1

1

The bean has to be placed in the view scope in order to get it to work.

@ManagedBean
@ViewScoped
public class FormSettings {}

Enabling the button by ajax counts as one HTTP request. Submitting the form with the button thereafter counts as another HTTP request. As your bean is request scoped, it get freshly created on every HTTP request and garbaged by the end of request. As the boolean property defaults to false, the button get effectively disabled again when JSF is about to process the form submit on the second request.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thx a lot for your reply! This is the first time I ask a question in this site and you explained it very well. I appreciate your feedback :) – Alper Filiz Jul 11 '12 at 06:29