JSF Managed bean and managed property as needed?

I am new to JSF and wondered:

If I have a controller that handles all the work for this page and a bean that contains all the data for the specified page, is it necessary to have both

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

annotations on the controller and

 @ManagedBean(name="myBean") @SessionScoped 

bean shape?

+6
source share
2 answers

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

For each class you write @ManagedBean, a bean is created by the JSF implementation as and when it detects the use of a bean with a name (you can either separate the bean or leave it to JSF to use the default class name with the first character changed to lowercase ) The created object is placed on the map of the specified area. Each area has a map that it uses to store beans that have the specified area.

Now, if you need the values ​​of these beans in your controller, you must enter it using the ManagedProperty annotation. Note that you will need to provide the controller with the setter method for the managed Property.

So, in order to answer your question, managedBean annotations should tell the JSF implementation about managing the bean instance and store the values ​​in a table specific to the session area. And ManagedProperty annotations are needed to use this bean stored in the current session so that you can access all of its values.

+10
source

We use the @ManagedBean annotation to register a java bean using the JSF framework. This is a replacement for the faces-config.xml <managed-bean> element. Usually we do not use the name attribute, because by default it uses a simple class name with a camel.

We use @RequestScope and other area annotations to explicitly specify the area we want with the annotation. This is equivalent to specifying a <managed-bean-scope> xml entry. If you do not specify a scope, then by default will be @NoneScoped .

We use @ManagedProperty and specify an EL expression in its value attribute to use the JSF-provided dependency injection mechanism for JSF artifacts, such as other managed beans, with wider scopes and EL-defined variables such as param . We do this if we need entered values ​​in other JSF artifacts, most typically beans. Injection values ​​are available in the bean @PostConstruct -nomen method. This is an alternative to writing <managed-property> xml.

Summarizing. Use @ManagedBean @RequestScoped to register beans using the JSF framework. Use @ManagedProperty inside this bean to be able to reference among other other JSF beans with the same or wider areas in this bean. If you do not need to reference other beans in the created bean, you do not need to use the @ManagedProperty annotation, since it is optional.

+6
source

Source: https://habr.com/ru/post/959459/


All Articles