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;
public class HelloBean implements Serializable {
	private static final long serialVersionUID = 1L;
	private String name;
	public String getName() {
		return name;
	public void setName(String 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"?>
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.



Download Source Code

Download it – (10KB)
Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Pingback: payment plan()

  • Pingback: payment plan()

  • Pingback: electricians salary in texas()

  • Pingback: house blue()

  • Pingback: locksmith vs dizaster twitter()

  • Pingback: their website()

  • Pingback: discover more here()

  • Pingback: caut electrician bucuresti()

  • Pingback: paypal loans()

  • Pingback: water ionizer plans()

  • Pingback: pay day loans()

  • Pingback: parking()

  • Pingback: Instagram followers kopen()

  • Pingback: laan nu og her()

  • Pingback: water ionizers()

  • Pingback: nu()

  • Pingback: sms laan nu()

  • Pingback: penge lan nu()

  • Pingback: parking()

  • Pingback:

  • Pingback:

  • Pingback: get satellite tv()

  • Pingback: Deep gullet three()

  • Pingback: best online casinos()

  • Pingback: stream movies()

  • Pingback: watch movies online free()

  • Pingback: Blue Coaster33()

  • pWoz

    I’m looking for similar solution for CDI bean. Is there any?

    Here is question:

  • Daniel Meza

    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 ?

  • Rakesh

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

  • ajay

    Why use this line below in bean

    Private static final long serialVersionUID = 1l;

    • Puneet

      In the above example the class is implementing Serializable interface

      public class HelloBean implements Serializable …

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

  • soufhard

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

  • maro

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

    &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; 
    &lt;html xmlns=&quot;;
    &lt;h:commandButton value=&quot;Click Here&quot; action=&quot;index&quot;&gt;&lt;/h:commandButton&gt;

    and this face-config.xml


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

  • scuddy

    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: WEB-INF/faces-beans.xml

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


    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:, javax.servlet.ServletResponse)

    Again Great job!