- Java EE 8 Application Development
- David R. Heffelfinger
- 371字
- 2021-07-02 22:05:01
Named beans
There are two types of Java Beans that can interact with JSF pages: JSF managed beans and CDI named beans. JSF managed beans have been around since the first version of the JSF specification and can be used only in a JSF context. CDI named beans were introduced in Java EE 6 and can interoperate with other Java EE APIs, such as Enterprise JavaBeans. For this reason, CDI named beans are preferred over JSF managed beans.
To make a Java class a CDI named bean, all we need to do is make sure the class has a public, no argument constructor (one is created implicitly if there are no other constructors declared, which is the case in our example) and add the @Named annotation at the class level. Here is the managed bean for our example:
package net.ensode.glassfishbook.jsf;
import javax.enterprise.context.RequestScoped; import javax.inject.Named; @Named @RequestScoped public class Customer { private String firstName; private String lastName; private String email; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
The @Named class annotation designates this bean as a CDI named bean. This annotation has an optional value attribute we can use to give our bean a logical name for use in our JSF pages. However, by convention, the value of this attribute is the same as the class name (Customer, in our case), with its first character switched to lower case. In our example, we let this default behavior take place, therefore we access our bean's properties via the customer logical name. Notice the value attribute of any of the input fields in our example page to see this logical name in action.
Notice that, other than the @Named and @RequestScoped annotations, there is nothing special about this bean. It is a standard JavaBean with private properties and corresponding getter and setter methods. The @RequestScoped annotation specifies that the bean should live through a single request. The different named bean, scopes, available for our JSF applications, are covered in the next section.
- Learning Java Functional Programming
- OpenDaylight Cookbook
- Mastering Objectoriented Python
- Learning RxJava
- 營(yíng)銷數(shù)據(jù)科學(xué):用R和Python進(jìn)行預(yù)測(cè)分析的建模技術(shù)
- Java虛擬機(jī)字節(jié)碼:從入門到實(shí)戰(zhàn)
- 云計(jì)算通俗講義(第3版)
- Python機(jī)器學(xué)習(xí)實(shí)戰(zhàn)
- Python機(jī)器學(xué)習(xí)編程與實(shí)戰(zhàn)
- Building a Quadcopter with Arduino
- 青少年學(xué)Python(第1冊(cè))
- The DevOps 2.5 Toolkit
- 運(yùn)用后端技術(shù)處理業(yè)務(wù)邏輯(藍(lán)橋杯軟件大賽培訓(xùn)教材-Java方向)
- 編程菜鳥學(xué)Python數(shù)據(jù)分析
- Python語言實(shí)用教程