JSF: How to Reference Managed Beans

Asked

Viewed 275 times

1

I’m starting to study JSF and soon came to me the question of how to reference the View (JSF) with Managed Beans. Because in the examples I see I can’t understand how JSF can see the Bean. For example:

Xhtml file:

...
<p:outputLabel value="Nome" for="nome"/>
<p:inputText id="nome" required="true" value="#{perfilUsuarioBean.nome}"/>
...

Arquivobean.java:

@ManagedBean
public class PerfilUsuarioBean {
    ...
    private String nome;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }        
    ...
}

How JSF Can See Bean Methods?

1 answer

3

Note that in this example the name of the bean is PerfilUsuarioBean, the view can see Bean by its name only without Camelcase.

In your case you can make the input Binding with the bean as follows:

value="#{perfilUsuarioBean.nome}"

Within the value we use EL Expression (Expression Language) which are this set of symbols #{}, inside you pass the name of your Bean and the value you want to make the link (Binding).

NOTE: You can change the name of your Manegedbean inside the annotation @ManagedBean:

@ManagedBean(name = "meuBean")
  • Thanks Diego, that I understood. I’m not getting access to Bean. I tried to do <h:outputText value="#{searchProducts.ola}" />, but it does not return to String. What I find strange is that you don’t have to specify where Bean is.

  • @Raphael , for JSF to find the variable ola in your Managedbean, it is necessary that Managedbean has the getter of the variable ola. Ex: public String getOla() { return this.ola; }

  • @igorventurelli I tried this way, but without success :( The bean I created is in the package with.empresa.project.controller JSF will be able to find it on its own?

  • @Raphael you have assigned a value to the variable ola? If you do public String getOla() { return "Ola Mundo"; } works?

  • @igorventurelli Yes, I did it this way and it didn’t work. I also initialized the variable in the constructor and it wasn’t.

Browser other questions tagged

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