Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind p:selectBooleanCheckbox to a controller method

Tags:

jsf

primefaces

I want to bind <p:selectBooleanCheckbox> to my controller method. I am getting below error when I try to bind. Is there any other way?

Property 'calculateBooleanValue' not found on type tr.controller.MyController

Here is my view

<p:datatable var="dataRow" ....>
    <p:selectBooleanCheckbox value="#{myController.calculateBooleanValue(dataRow)}" />

MyController

public boolean calculateBooleanValue(Data data) {

 //There are some calculations with data and returns some boolean value acc. to this data


 }
like image 481
hellzone Avatar asked Mar 11 '26 18:03

hellzone


2 Answers

The value attribute must represent a value expression. I.e. it must be bound to a property which is represented by a true javabean getter and setter. You should not use method expression syntax with parentheses, this is not a valid value expression syntax.

Thus, so:

<p:selectBooleanCheckbox value="#{myController.booleanValue}" />

with

private boolean booleanValue;

@PostConstruct
public void init() {
    booleanValue = true;
}

public boolean isBooleanValue() {
    return booleanValue;
}

public void setBooleanValue(boolean booleanValue) {
    this.booleanValue = booleanValue;
}

If you intend to execute some "controller method" (as you call it yourself) on every click of the checkbox, then add a <p:ajax> with a listener:

<p:selectBooleanCheckbox value="#{myController.booleanValue}">
    <p:ajax listener="#{myController.changeBooleanValue}" />
</p:selectBooleanCheckbox>

with

public void changeBooleanValue() {
    System.out.println("Current boolean value is: " + booleanValue);
    // ... 
    // Do your job here.
}
like image 118
BalusC Avatar answered Mar 14 '26 19:03

BalusC


You can't. value takes a Value Expression, not a Method Expression.

Think about it, how is the property going to be written, if it's a method?

like image 23
RinaldoDev Avatar answered Mar 14 '26 18:03

RinaldoDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!