Use or not use "Backing Bean"/"Manage Bean" with JSF?

Asked

Viewed 390 times

2

On page 64 of the Algaworks e-book "Java EE 7, with JSF, Primefaces and CDI", explains the use of the backing Beans, which are managers Beans, responsible for controlling the components of the JSF pages.

But at the end of the explanation there is a paragraph that says:

"Although powerful, this feature should be used very carefully. The use excessive can make the source code large and difficult to understand. Most of the time, we can do what we need using only value link expressions. "

So my question is this, by default I should or should not use the backing Beans? I say I will build the application and I want to set a pattern, so the right thing is every page has a backing bean, or just the most "complex" the rest I should treat in a Manage-bean common.

2 answers

0


It depends a lot on your application, I recommend using a @ManagedBean and set a life cycle for your bean(@ViewScoped, @RequestEscoped or @SessionScoped), with this you can already develop a quiet application.

0

Diego Santos,

The ManagedBean would be your Controller of the application, our View .xhtml in case you get access to our properties in ManagedBean or in our BB as in the example below:

Statement of ManagedBean ( From version 2.0 we can do via Annotations before that I just saw .xml.

@ManagedBean(name = "comentario")
@ViewScoped <= Definição do escopo do ciclo de vida do seu ManagedBean
public class CComentario 
{
 private List<Anexo> listaAnexos; //Uma lista de Anexos

}

In his View we can perform validations such as :

  • Render components
  • Block access
  • Among others.

To perform a validation using the list available in Managedbean we can do as follows:

    <p:outputPanel rendered="#{fn:length(comentario.anexos) gt 0}">
       //aqui os elementos que serão renderizados
    </p:outputPanel>

We can also access attributes defined in your Backing bean

<strong> Comentário #{CComentario.comentario.descricao} </strong>

It is well worth studying the life cycle of each scope we have available as:

@ApplicationScoped: Will be available all the time until the application "drops"

@SessionScoped: Becomes available as long as the session is validated.

@ViewScoped: It is only available on the page where it was declared.

@RequestScoped: Available between a single http call.

We also :

@CustomScoped: Where you can create a custom scope.

@FlowScoped: You set the flow of life for example :Enter the display case > Choose product > Purchase product > End . This available jsf 2.2 only up

Browser other questions tagged

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