• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JSF 2 Managed Beans Example

April 3, 2014 //  by Amr Mohammed//  Leave a Comment

The concept of managed bean(s) or backing bean(s) is used intensively by the jsf developers for referring that/those Java Class(es) which are used for handling the presentation business logic. Concept of managed bean(s) are coming from a more general concept that introduced before many years ago and it’s an MVC (Model, View & Controller).

In a JSF framework the managed beans are considered the model and the controller at the same time, in that the managed beans are associated with the view through what you can called (JSF expression language) and they are determine the navigation outcomes that are consumed by the JSF navigation handler for specifying the next coming view that the user should laying on. Jsf is beyond the boundaries of just a UI framework, it’s considered as a complete framework, so it’s more expected to see such that fusion.

The Managed beans scoping has been defined using <managed-bean-scope/> xml fragment. In jsf 2 that time is gone, and now you have the new annotations for defining and specifying the scopes. Using annotations such @SessionScoped and @RequestScoped and other like them are considered the best ever way for configure the manage beans in the desired scopes.

In the previous releases of jsf and before jsf 2 coming into existence, the managed beans were defined by providing the <managed-bean/> xml fragment into the jsf configuration file (faces-config.xml), but since jsf 2 the managed beans are defined using the old one and using a new fashion way, through the using of annotations. By that no need for doing some tedious work that you are pretty sure happy if you’ve ignored, so instead of using <managed-bean/> xml fragment you can consider the annotations @ManagedBean as an alternative fragment of code that you’ve to add.

Jsf framework doesn’t stop at this level, it’s also provide an unbelievable way for injecting a bean into another bean using the most shortcut way, through using the @ManagedProperty annotation. Also, it now be the right time for covering a newly concept introduced by the jsf 2 and it related to the managed beans lifecycle.

This tutorial intended for providing a complete example for all the possibilities that are used for defining and configuring the managed beans at the jsf 2.0.

Also Read:

  • JSF 2 Tutorials
  • JSF Tutorials
  • Introduction to JSF

1. JSF Faces Configuration

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
	version="2.2">
<application>
	<resource-bundle>
		<base-name>net.javabeat.jsf.application</base-name>
		<var>msg</var>
	</resource-bundle>
</application>
<managed-bean>
	<managed-bean-name>jpaTutorialBean</managed-bean-name>
	<managed-bean-class>net.javabeat.jsf.JPATutorialBean</managed-bean-class>
	<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
	<managed-bean-name>jsfTutorialBean</managed-bean-name>
	<managed-bean-class>net.javabeat.jsf.JSFTutorialBean</managed-bean-class>
	<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
  • JPATutorialBean and JSFTutorialBean are defined within faces-config.xml.

2. JPATutorialBean

JPATutorialBean.java

package net.javabeat.jsf;

public class JPATutorialBean {
	private String message ="This is a JPATutorialBean";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
  • No annotations are used to configure JPATutorialBean as a managed bean.

3. JSFTutorialBean

JSFTutorialBean.java

package net.javabeat.jsf;

public class JSFTutorialBean {
	private String message = "This is a JSF Tutorial Bean";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
  • No annotations used for configuring JSFTutorialBean as a managed bean

4. IndexBean

IndexBean.java

package net.javabeat.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class IndexBean {
	private String message = "This is an IndexBean";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
  • @ManagedBean is used to configure the IndexBean as a managed bean
  • @RequestScoped, @SessionScoped and @ApplicationScoped are the most popular scopes for determining the scope of the bean.
  • IndexBean class hasn’t mentioned in the faces-config.xml

5. The View

index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">
<h:head>
	<h:outputScript library="javax.faces" name="jsf.js"/>
</h:head>
<h:body>
	<f:view>
		<h1>JavaBeat JSF 2.2 Examples</h1>
		<h2>JSF2 Configuring Managed Bean Example</h2>
		<h:form prependId="false">
			<h:outputText style="color:red" value="Index Bean Message : #{indexBean.message}"></h:outputText>
			<br/>
			<h:outputText style="color:blue" value="JPATutorial Bean Message : #{jpaTutorialBean.message}"></h:outputText>
			<br/>
			<h:outputText style="color:green" value="JSFTutorial Bean Message : #{jsfTutorialBean.message}"></h:outputText>
		</h:form>
	</f:view>
</h:body>
</html>

6. The Deployment Descriptor (web.xml)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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" metadata-complete="true">
	<context-param>
		<description>State saving method: 'client' or 'server'
						(=default). See JSF Specification 2.5.2
		</description>
		<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
		<param-value>client</param-value>
	</context-param>
	<context-param>
		<param-name>javax.faces.application.CONFIG_FILES</param-name>
		<param-value>/WEB-INF/faces-config.xml</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>/faces/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.xhtml</url-pattern>
	</servlet-mapping>
	<listener>
		<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
	</listener>
</web-app>
  • The defined managed bean inside faces-config.xml will not binded if you’ve omit the context-param javax.faces.application.CONFIG_FILES

7. Configuring Managed Bean Demo

The below snapshot shows you how could managed beans configured for being used.
JSF 2 Configuring Managed Beans Example 1

[wpdm_file id=35]

Category: JSFTag: JSF 2

About Amr Mohammed

Previous Post: « JSF 2 Ajax JavaScript Library Example
Next Post: JSF 2 Injecting Managed Beans Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact