Configure Managed Beans in JSF 2.0

In JSF 2.0, 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 (a bean contains all the HTML form value).

There are two ways to configure the managed bean :

1. Configure Managed Bean with Annotation

In JSF 2.0, you can annotated a Managed Bean with new @ManagedBean annotation.


package com.mkyong.common;
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;
	}
}

2. Configure Managed Bean with XML

With XML configuration, you can use the old JSF 1.x mechanism to define the managed bean in a normal faces-config.xml file.


<?xml version="1.0" encoding="UTF-8"?>
<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">
    <managed-bean>
	  <managed-bean-name>helloBean</managed-bean-name>
	  <managed-bean-class>com.mkyong.common.HelloBean</managed-bean-class>
	  <managed-bean-scope>session</managed-bean-scope>
     </managed-bean>
</faces-config>
Best Practice
It’s recommended to put the managed beans in a separate XML file because the faces-config.xml is used to set the application level configurations.

So, you should create a new XML file and put the managed beans detail inside, and declared the XML file in the javax.faces.CONFIG_FILES initialize parameter, which is inside the WEB-INF/web.xml file.

web.xml


 ...
 <context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>WEB-INF/manage-beans.xml</param-value>
  </context-param>
...

Download Source Code

Download it – JSF-2-Managed-Beans-Example.zip (10KB)

15 comments on “Configure Managed Beans in JSF 2.0

  1. Hello mkyong,

    I have been able to create index.xhtml but when I create Managed Bean I run into compilation error even thought I did everything right. One thing I noticed is that I don’t see JSF Managed Bean either, instead I see JSF CDI bean. what do you thing the issue might be?

    Reply
  2. security loop hole in this comment session, please Improve you comment session.

    Reply
  3. I have problem with serialization, certain object can’t be serializabled, i use them in a ViewScoped ManagedBean, do you think i must implements Serializable in my ViewScoped beans or it is unuseful ?

    Reply
  4. Hi I have a doubt.I’ve followed your instructions on this tutorial but only the second approach works for me ( using faces-config ) the annotation way never has worked for me,am I missing something ?

    Reply
    1. I am also trying to figure out the same as I am also facing the same issue. Please enlighten me on the same if you found the answer to the question. Thanks in advance 🙂

      Reply
  5. Can u please tell me the difference between Backing Bean and Manged Bean ?
    Is both are same ..?
    Please replay ..

    Reply
  6. Why use this line below in bean

    Private static final long serialVersionUID = 1l;

    Reply
    1. In the above example the class is implementing Serializable interface

      @ManagedBean
      @SessionScoped
      public class HelloBean implements Serializable …

      by default if the class is implementing Serializable,we need to provide serialVersionUID with in the class.

      Reply
  7. Hello Hi Mkyong,
    thanks for you tutorials,it’s very helping ! i was wondering how to create an abstract bean on xml file thansk!

    Reply
  8. Hi Mkyong,
    I have a question can you help me ,I got this when i try to run my first jsf application

    org.apache.myfaces.config.annotation.TomcatAnnotationLifecycleProvider destroyInstance
    INFO: Destroy instance of com.jsf2tut.model.MyBean

     
    
        package com.jsf2tut.model;
        
        import javax.faces.bean.ManagedBean;
        import javax.faces.bean.SessionScoped;
        
        @ManagedBean(name="myBean")
        @SessionScoped
        
        public class MyBean {
        	
        	private String myName;
        
        	public String getMyName() {
        		return myName;
        	}
        
        	public void setMyName(String myName) {
        		this.myName= myName;
        	}
        	
        }
     

    I have another question please, I have a simple file called login.xhtml

     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <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:commandButton value="Click Here" action="index"></h:commandButton>
    </html>
    

    and this face-config.xml

    	<navigation-rule>
    		<from-view-id>/login.xhtml</from-view-id>
    		<navigation-case>
    			<from-outcome>index</from-outcome>
    			<to-view-id>/index.xhtml</to-view-id>
    		
    		</navigation-case>
    	</navigation-rule>
    

    when click on “Click here” button do nothing why?

    Reply
  9. Hi Mkyong,
    Great tutorials! I tried this on a jetty server and found that the code snippet for splitting the config files fails with the following error:

    SEVERE: Critical error during deployment:
    com.sun.faces.config.ConfigurationException: java.util.concurrent.ExecutionException: javax.faces.FacesException: java.net.MalformedURLException: WEB-INF/faces-beans.xml

    After some research I figured out that the path is incorrect, It should be:

    <context-param>
      <param-name>javax.faces.CONFIG_FILES</param-name>
      <param-value>/WEB-INF/faces-beans.xml</param-value>
    </context-param>
    

    So what i feel is that param-value needs to be set with root context (/WEB-INF/) and not with relative path (WEB-INF/).
    Found from here: http://docs.oracle.com/javaee/6/api/javax/faces/webapp/FacesServlet.html#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

    Thanks,
    Again Great job!

    Reply
    1. you are right scuddy…It took some for me to figure that out.

      Reply

Leave a Comment

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