CHAPTER 6: Component-Based Web Development Using JSF 2 275
Line 10: When the page is submitted, JSF will find the helloBean and set
the submitted name value via the setName() method. When hello.xhtml is
displayed, JSF will find the helloBean and display the name property value via its
getName() method.
Listing 6-3 illustrates the helloBean.
Listing 6-3. Managed Bean
- package com.apress.jsf.helloworld;
- import javax.faces.bean.ManagedBean;
- import javax.faces.bean.SessionScoped;
- import java.io.Serializable;
- @ManagedBean
- @SessionScoped
- public class HelloBean implements Serializable {
- private static final long serialVersionUID = 1L;
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
Line 6: A managed bean is a Java bean that is accessed from a JSF page. The
@ManagedBean annotation specifies the name by which an object of this class is
referenced in the JSF pages.
Line 7: A managed bean must have a name and a scope. Session scope
signifies that the bean object is available for one user across multiple pages.
Note There are two annotations for naming a bean. @Named is the best choice with a Java EE 6 and newer
application server.
Like Struts and Spring MVC web applications, when you deploy a JSF web application
inside an application server, you need to provide a deployment descriptor file named web.xml.
Listing 6-4 shows this file. For the sake of brevity, the web-app declaration and the list of files in the
welcome-file-list are not completely shown.