简体   繁体   中英

Set bean value on click of selectbooleancheckbox

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.

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, ie 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, ie:

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

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