简体   繁体   中英

commandButton which is enabled by f:ajax does not invoke action when form is submitted

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>

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM