Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF Managed bean and managed property both necessary?

I'm new to JSF and was wondering:

If I have a controller that handles all the work for a given page and a bean that holds all the data for said page, is It necessary to have both the

@ManagedProperty(value="#{myBean}") 

annotation on the controller and the

@ManagedBean(name="myBean")
@SessionScoped

annotations on the form bean?

like image 540
Mark W Avatar asked Dec 06 '13 10:12

Mark W


People also ask

What is managed property in JSF?

Annotation Type ManagedProperty. The presence of this annotation on a field of a class annotated with ManagedBean instructs the system to inject a value into this property as described in section JSF. 5.3 of the spec prose document in the <managed-property> subsection.

Why do we need to define 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.

Which scope is used on managed beans which is created for every session?

ApplicationScope: The application scope persists for the entire duration of the web application. That scope is shared among all requests and all sessions. You place managed beans into the application scope if a single bean should be shared among all instances of a web application.

What is a managed bean in JSF?

A managed bean is a normal java bean class which contains data and business logic. A managed bean is managed by JSF framework and act as a model for JSF application. Before using a managed bean we have to configure it either in faces-config.xml file or using annotation.

How to use @ManagedProperty in JSF?

This example shows how to use jsf managed property. When annotating a field with @ManagedProperty the values are set immediately after the bean’s construction and are available during @PostConstruct. Be informed that you can only use @ManagedProperty with beans managed by JSF @ManagedBean, not with beans managed by CDI @Named.

What is the difference between managed bean and backing bean?

A managed bean is managed by JSF framework and act as a model for JSF application. Before using a managed bean we have to configure it either in faces-config.xml file or using annotation. JSF backing bean is a managed bean which contains some or all values of a web form. It is normally for business logic.

What is @ManagedBean in Salesforce?

@ManagedBean marks a bean to be a managed bean with the name specified in name attribute. If the name attribute is not specified, then the managed bean name will default to class name portion of the fully qualified class name. In our case, it would be helloWorld.


2 Answers

Managed beans in JSF are used to store the state of a web page. The JSF implementation is responsible for creating and discarding the bean objects( hence the name managed bean).

For every class you write @ManagedBean, the bean object is created by the JSF implementation as and when it detects an usage of the bean with the name(you can either sepcify a bean name or leave it to JSF to use the default name-class name with the first character changed to lowercase). The object created is placed in a map of the specified scope. Each scope has a map that it uses to store bean objects which have that scope specified.

Now if you need the values of these beans in your controller, you have to inject it using the ManagedProperty annotation. Note that you would need to provide the controller with a setter method for the managedProperty.

So to answer your question, the managedBean annotation is required to tell the JSF implementation to manage the bean instance and store the values in the table specific to the session scope. And the ManagedProperty annotation is needed to use that bean stored in the current session so that you can access all of its values.

like image 62
Adarsh Avatar answered Oct 25 '22 18:10

Adarsh


We use @ManagedBean annotation to register a java bean with a JSF framework. This is a replacement for a faces-config.xml <managed-bean> element. We typically do not use name attribute because it already defaults to a simple class name camel cased.

We use @RequestScope and other scope annotations to explicitly specify the scope we want via annotation. This is equivalent to specifying<managed-bean-scope> xml entry. If you don't specify the scope it will be defaulted to @NoneScoped.

We use @ManagedProperty and specify an EL-expression in its value attribute to use JSF-provided dependency injection engine for JSF artifacts like other managed beans with broader scopes and EL-defined variables like param. We do it in case we need the injected values in other JSF artifacts, most typically beans. The injected values are available in bean's @PostConstruct-annotated method. This is an alternative to <managed-property> xml entry.

To sum it up. Use @ManagedBean @RequestScoped to register a bean with JSF framework. Use @ManagedProperty inside this bean to be able to reference among others other JSF beans with the same or broader scopes in this bean. In case you don't need to reference other beans in the created bean you don't need to use the @ManagedProperty annotation as it's purely optional.

like image 41
skuntsel Avatar answered Oct 25 '22 18:10

skuntsel