JSF 2 Injecting Managed Beans Example

This tutorial intended for providing a complete example of how could wire (connect/inject/reference) a managed beans from/into another managed bean. In our previous tutorial I have explained how to configure a managed bean.

Also Read:

1. IndexBean Managed Bean

IndexBean.java

package net.javabeat.jsf;

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

@ManagedBean
@SessionScoped
public class IndexBean {
	private String message = "We Have Two Injected Beans :";
	@ManagedProperty(value="#{jpaTutorialBean}")
	private JPATutorialBean jpa;
	@ManagedProperty(value="#{jsfTutorialBean}")
	private JSFTutorialBean jsf;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public JPATutorialBean getJpa() {
		return jpa;
	}

	public void setJpa(JPATutorialBean jpa) {
		this.jpa = jpa;
	}

	public JSFTutorialBean getJsf() {
		return jsf;
	}

	public void setJsf(JSFTutorialBean jsf) {
		this.jsf = jsf;
	}

	public String display(){
		this.message = this.message + " "+this.jpa+" And "+this.jsf;
		return "";
	}
}

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;
	}
}

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;
	}
}

4. 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>

5. 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>

6. 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 Injecting Managed Bean Example</h2>
		<h:form prependId="false">
			<h:commandButton value="List All Injected Beans" action="#{indexBean.display}"></h:commandButton>
			<h:outputText style="color:black" value="#{indexBean.message}"></h:outputText>
		</h:form>
	</f:view>
</h:body>
</html>

7. JSF 2 Injecting Managed Beans Demo

The below snapshot shows you a message print out all injected beans instances.

JSF 2 Injecting Managed Bean Example

Comments

comments

About Amr Mohammed

Comments

  1. Hey please remove < and > from xml file lol

Speak Your Mind

*