JSF - Managed Beans

What Is Managed Beans in JSF ?

Java bean that can be accessed from JSF page is called Managed Bean. The managed bean can be a normal Java bean, which contains the getter and setter methods, business logic or even a backing bean.

Managed beans works as Model for UI component.

In JSF 1.2,a managed bean had to register it in JSF configuration file such as faces-config.xml

From JSF 2.0 onwards, Managed beans can be easily registered using annotations. This approach keeps beans and there registration at one place and it becomes easier to manage.

What Is Managed Beans in JSF ?

Java bean that can be accessed from JSF page is called Managed Bean. The managed bean can be a normal Java bean, which contains the getter and setter methods, business logic or even a backing bean.

Managed beans works as Model for UI component.

In JSF 1.2,a managed bean had to register it in JSF configuration file such as faces-config.xml

From JSF 2.0 onwards, Managed beans can be easily registered using annotations. This approach keeps beans and there registration at one place and it becomes easier to manage.




Managed Bean Using XML Configuration

<managed-bean>
  <managed-bean-name>helloWorld</managed-bean-name>
  <managed-bean-class>com.jwt.jsf.HelloWorld</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean> 
<managed-bean>
  <managed-bean-name>employee</managed-bean-name>
  <managed-bean-class>com.jwt.jsf.Employee</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean> 

To configure the bean in configuration file <managed-bean> element is used. Its child element <managed-bean-name> is used to give the name of the bean to be used in the application to refer the bean class specified in another child element <managed-bean-class>.


Managed Bean Using Annotation

@ManagedBean(name = "hello", eager = true)
@RequestScoped
public class HelloWorld {
	
   @ManagedProperty(value="#{message}")
   private Message message;
   ...
}

@ManagedBean Annotation

@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. In our case it would be hello.

Another important attribute is eager. If eager="true" then managed bean is created before it is requested for the first time otherwise "lazy" initialization is used in which bean will be created only when it is requested.

Scope Annotations

Scope annotations set the scope into which the managed bean will be placed. If scope is not specified then bean will default to request scope. Some important scope is briefly discussed below.

  • @RequestScoped :- Bean lives as long as the HTTP request-response lives.

  • @SessionScoped :-Bean lives as long as the HTTP session lives. It get created upon the first HTTP request involving this bean in the session and get destroyed when the HTTP session is invalidated.

  • @ApplicationScoped :-Bean lives as long as the web application lives. It get created upon the first HTTP request involving this bean in the application (or when the web application starts up and the eager=true attribute is set in @ManagedBean) and get destroyed when the web application shuts down.

Let's see one example for JSF Managed Bean.


1 . Managed Bean Class

EmployeeBean.java

package com.javawebtutor.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "empBean", eager = true)
@RequestScoped
public class EmployeeBean {
	private String empName;
	private String details;
	private String email;
	private int phone;

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = "Please Provide Employee Name";
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getPhone() {
		return phone;
	}

	public void setPhone(int phone) {
		this.phone = phone;
	}

	public void nameService() {
		empName = empName.toUpperCase();
	}

}

In the above code we have declared our Bean as ManagedBean by using the annotation @ManagedBean.

We can set the attribute name to define how we will reference the bean in our Jsp or JSF pages.The attribute eager = true, means bean is created before it is requested for the first time otherwise lazy initialization is used in which bean will be created only when it is requested.

If we are not specifying the name attribute, the Bean will be bound using the Class name, with the first letter in lowercase. If you don't specify the eager attribute it will default to false.

The other annotation we have added is @RequestScoped which means the Bean is bound in the Request.



2 . JSP Page

index.jsp

<%@ page contentType="text/html"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<f:view>
	<html>
<head>
</head>
<body bgcolor="silver">

	<h:form id="helloForm">
		<h2>Enter employee name</h2>

		<h:inputText id="name" value="#{empBean.empName}" />
		<h:commandButton value="go" action="#{empBean.nameService}" />

	</h:form>
</body>
	</html>
</f:view>

Note:- JSF preferred view technology is Facelets.Facelets are generally created as .XHTML pages supporting a special set of Tag Libraries.JSP technology is considered to be a deprecated presentation technology for Java Server Faces 2.0.


3 . web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<context-param>
		<param-name>com.sun.faces.enableRestoreView11Compatibility</param-name>
		<param-value>true</param-value>
	</context-param>

	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.jsf</url-pattern>
	</servlet-mapping>
</web-app>

4 . faces-config.xml

<faces-config xmlns="http://java.sun.com/xml/ns/javaee" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" 
          version="2.0"> 
</faces-config>

Note :-In the above code attribute version="2.0" is specified. If by mistake a faces-config.xml with the attribute set to "1.2" or "1.0", the code will run with the earlier JSF release.



comments powered by Disqus