• JavaScript
  • Core Java
  • PHP
  • Struts
  • SQL Tutorial
  • Spring
  • Servlet
  • jQuery

  • <

    JSF Managed Bean

    It is a pure Java class which contains set of properties and set of getter, setter methods.

    Following are the common functions that managed bean methods perform:

  • Validating a component's data

  • Handling an event fired by a component

  • Performing processing to determine the next page to which the application must navigate

  • It also works as model for JFS Framework.

    The @ManagedBean annotation in a class automatically registers that class as a resource with the JavaServer Faces. Such a registered managed bean does not need managed-bean configuration entries in the application configuration resource file.

    This is an alternative to the application configuration resource file approach and reduce the task of configuring managed beans.

    The @RequestScoped annotation is used to provide scope for ManagedBean. You can use annotations to define the scope in which the bean will be stored.

    You can use following scopes for a bean class:

  • Application (@ApplicationScoped): Application scope persists across all users? interactions with a web application.

  • Session (@SessionScoped): Session scope persists across multiple HTTP requests in a web application.

  • View (@ViewScoped): View scope persists during a user?s interaction with a single page (view) of a web application.

  • Request (@RequestScoped): Request scope persists during a single HTTP request in a web application.

  • None (@NoneScoped): Indicates a scope is not defined for the application.

  • Custom (@CustomScoped): A user-defined, nonstandard scope. Its value must be configured as a java.util.Map. Custom scopes are used infrequently.

  • Eager Managed Bean

    Managed bean is lazy by default. It means, bean is instantiated only when a request is made from the application.

    You can force a bean to be instantiated and placed in the application scope as soon as the application is started.


    package com.javameant.test;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ManagedProperty;
    import javax.faces.bean.RequestScoped;
    @ManagedBean(name = "helloWorld", eager = true)
    public class HelloWorld {
       @ManagedProperty(value = "#{message}")
       private Message messageBean;
       private String message;
       public HelloWorld() {
          System.out.println("HelloWorld started!");   
       public String getMessage() {
          if(messageBean != null) {
             message = messageBean.getMessage();
          return message;
       public void setMessageBean(Message message) {
          this.messageBean = message;

    package com.javameant.test;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    @ManagedBean(name = "message", eager = true)
    public class Message {
       private String message = "Hello World!";
       public String getMessage() {
          return message;
       public void setMessage(String message) {
          this.message = message;

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    <html xmlns = "http://www.w3.org/1999/xhtml">
          <title>JSF Tutorial!</title>

    © copyright 2017-2021 Completedone pvt ltd.