Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a managed bean and how many should I use per page?

I am working on a Web-application using JavaServer Faces.

I have found many examples and tutorials on how to use JavaServer Faces, but none of them actually explain what a Bean is used for. My initial thoughts were, that Beans represent forms. You enter data into your form and click the submit button and the associated Bean is filled with the data and a method is called.

However, so far I have only seen examples where there is one Bean per page, so a Bean could also represent a page and thus contain multiple forms.

I am also confused about the scope of a Bean. If the Bean represents a form or a page, it should become invalid after the request ended. If you make the bean live in the scope of the session, what happens to the Bean? Are you still able to somehow get the data out of it or will it just fill the associated form for you once you go back to it?

In summary - what is a Managed Bean and how do you use it properly?

like image 805
Xandaros Avatar asked Dec 06 '13 17:12

Xandaros


People also ask

What is a managed bean?

Managed Bean is a regular Java Bean class registered with JSF. In other words, Managed Beans is a Java bean managed by JSF framework. Managed bean contains the getter and setter methods, business logic, or even a backing bean (a bean contains all the HTML form value). Managed beans works as Model for UI component.

What is the difference between managed bean and backing bean?

1) BB: A backing bean is any bean that is referenced by a form. MB: A managed bean is a backing bean that has been registered with JSF (in faces-config. xml) and it automatically created (and optionally initialized) by JSF when it is needed.

What scopes are available for a managed bean?

You can use following scopes for a bean class: Application (@ApplicationScoped): Application scope persists across all users? interactions with a web application. Session (@SessionScoped): Session scope persists across multiple HTTP requests in a web application.

What is managed bean in spring?

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.


1 Answers

A bean is a managed bean when you never need to manage the bean instance yourself as in by manually doing someScopedMap.put("bean", new Bean());. The framework takes care of this. This has nothing to do with whether the page contains a form or not. You're basically telling the framework "Hey, here's a backing bean class com.example.Foo. You may use it as managed bean with name "foo" and put it in scope X whenever an EL expression references it via #{foo}", without the need to do that all yourself. The framework (JSF, CDI, Spring, etc) manages it all by itself.

As to the choosing the right scope, that logically depends on the scope the data needs to be kept in. For example, you obviously don't want to store submitted form data in application scope. Any other visitor of the website would otherwise see it. Just think logically and read How to choose the right bean scope?

As to "one bean per form" or "one bean per page" concept, this is actually subjective as both can technically work equally good, but as to maintainability it's strongly recommended to make your classes as slick as possible, including backing beans. Therefore the "one bean per form" is the better practice. Don't throw completely different responsibilities together in a single class. Law of demeter and such. Beans can easily access each other using @Inject or @ManagedProperty.

See also:

  • JSF Controller, Service and DAO
  • How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?
  • Backing beans (@ManagedBean) or CDI Beans (@Named)?
like image 95
BalusC Avatar answered Nov 16 '22 02:11

BalusC