1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
code_fish
  • 3,381
  • 5
  • 47
  • 90

1 Answers1

6

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>
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • Its working. Thanks. Now I want to show/hide a combox box on basis of the value of this checkbox. You can check the question here http://stackoverflow.com/questions/25551303/jsf-ajax-render-not-working-on-selectbooleancheckbox. Sorry I am new to JSF. – code_fish Aug 29 '14 at 13:35
  • You're welcome! Use the `render` attribute of `` tag and follow what the `rendered` attribute of to-be-updated components is evaluated to. This will guide you in the right direction and you will solve the issue on your own. – skuntsel Aug 29 '14 at 13:40
  • Thanks !! I am doing like that only. But I am not able to find out why it's not working. I have checked many times. – code_fish Aug 29 '14 at 14:02