Set bean value on click of selectbooleancheckbox

code_fish picture code_fish · Aug 29, 2014 · Viewed 9k times · Source

I have a bean class and a selectBooleanCheckbox in xhtml page. I want that on the click of the box the value should be set in the backing bean.

Here is code:

<h:selectBooleanCheckbox id="provisioningTargetCollector" 
                           value="#{targetSource.provisioningTargetCollector}">
                           </h:selectBooleanCheckbox>

Bean Class:

public boolean isProvisioningTargetCollector() {
    return _provisioningTargetCollector;
 }

 public void setProvisioningTargetCollector(boolean provisioningTargetCollector) {
     _provisioningTargetCollector = provisioningTargetCollector;
 }

But the getter and setter are called only on page load. How can I set the value in bean method on click of checkbox.

Answer

skuntsel picture skuntsel · Aug 29, 2014

The model with be filled with form data only when submit button will be pressed. If you want to do partial update to the server you need to send an AJAX request. Luckily, starting from JSF 2 it has been quite simple with the introduction of <f:ajax> tag. It adds ajax capabilities to UIComponent instances that implement the ClientBehaviorHolder interface, i.e. components that are capable of triggering ajax requests.

To do partial update of compenets you need to specify their client ids in execute attribute of <f:ajax> tag. As the default value of execute attribute evaluates to @this, or the component to which the tag is attached it. As soon as you want to update only the given <h:selectBooleanCheckbox> you can do it as simple as nesting a pure <f:ajax /> tag within you checkbox, i.e.:

<h:selectBooleanCheckbox id="provisioningTargetCollector" value="#{targetSource.provisioningTargetCollector}">
    <f:ajax />
</h:selectBooleanCheckbox>