How to disable a commandButton in Primefaces according to the return of a Boolean method implemented in the bean?

Asked

Viewed 301 times

-1

I have a button to delete a file that I just want to appear to the user if my class has records. Just follow his code:

<p:commandButton icon="ui-icon-trash"
value="Excluir"
actionListener="#{deParaBean.deletar}"
oncomplete="PF('dlg1').hide"
update=":mainForm"
disabled="#{deParaBean.temRegistros}"
ajax="false"/>

I want his disabled property to receive true or false according to the temRegistros method I implemented in my bean. Follow his code:

public boolean temRegistros(){
        if (itemParceria == null) {
            return false;
        } else {
            return true;
        }
}

However, when I start the server and upload the application, before rendering the screen, the following error appears to me:

javax.servlet.Servletexception: /pages/depara.xhtml @111,23 disabled="#{deParaBean.temRegistros}": The class 'br.com.parceria.bean.Deparabean' does not have the Property 'temRegisters'.

Someone knows what I did wrong?

  • Try to rename the method to getTemRegisters, and keep disabled="#{deParaBean.temRegisters}"

1 answer

0

In "disabled" you must reference a property of your class and not a method.

Then, in your class, create the property "temRegistros":

private Boolean temRegistros;

Create get and set for this property.

And modify your method so it stays that way:

public boolean temRegistros(){
    if (itemParceria == null) {
        this.temRegistros = false;
    } else {
        this.temRegistros = true;
    }
}

It would be good to choose another name for the method. So it is easier to differentiate the method from the property.

Don’t forget to update the component using "update" after running the method that checks for records.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.