Java Server Faces: Managed Bean

User Interface (UI) components in a JSF page are often associated with JavaBeans (called backing beans) that provide application logic. A JSF page may also use beans for other purposes such as validations, conversions etc. Since these beans are created and managed by a JSF implementation, they are affectionately called managed beans. A managed bean is called a backing bean if it is bound with a UI component. Here is an example of a very simple managed bean (HeiioWorid.java):

package ukr;

import javax.faces.bean.ManagedBean;

@ManagedBean

public class HelloWorld {

public String getMessage() {

return “Hello World!”;

}

}

It has only one method getMessage() that returns the string Hello World!. The @ManagedBean annotation registers the bean to be managed with the FacesServlet implementation. A managed bean is connected to a web page through Expression Language (EL). The <h:outputText> component in our previous web page (HelloWorld.xhtml) may be connected to this bean as follows:

<h:outputText value=”#{helloWorld.message}” />

The EL # {helloWorld.message} retrieves the value of the message property (through getMessage() method) of the managed bean helloWorld. A detailed syntax of EL expressions is discussed later in this chapter. Note that the name helloWorld is used to refer to the managed bean HelloWorld. If no name is specified in the @ManagedBean annotation, the managed bean’s name becomes the class name with the first letter in lowercase.

To compile this file, we need the javax.faces.bean.ManagedBean annotation class which is included in the JSF library file javax.faces-2.2.8-04.jar. Specify this jar file using -cp option of javac command. For example, we placed this jar file in the same directory containing HelloWorld. java file and used the following command to compile

javac -cp javax.faces-2.2.8-03.jar HelloWorld.java

It generates the HelloWorld.class file. Create a directory ukr under JSF_HOME\WEB-INF\classes and copy HelloWorld.class file there. The final directory content will look exactly as shown in the following page.

The web application is now ready to deploy. One way to do this is to place the entire jsf/ directory in the Tomcat’s webapps folder. We can also create a .war file and place it in the Tomcat’s webapps directory. To create a .war file, go to the jsf / directory and use the following command:

jar cvf jsf.war *

A sample output of this command is shown below:

added manifest

adding: HelloWorld.xhtml(in = 364) (out= 242)(deflated 33%)

adding: WEB-INF/(in = 0) (out= 0)(stored 0%)

adding: WEB-INF/classes/(in = 0) (out= 0)(stored 0%)

adding: WEB-INF/classes/ukr/(in = 0) (out= 0)(stored 0%)

adding: WEB-INF/classes/ukr/HelloWorld.class(in = 366) (out= 270)(deflated 26%)

adding: WEB-INF/lib/(in = 0) (out= 0)(stored 0%)

adding: WEB-INF/lib/javax.faces-2.2.8-04.jar(in = 3127585) (out= 2905152)(deflated 7%)

adding: WEB-INF/web.xml(in = 748) (out= 344)(deflated 54%)

This jsf.war file contains all the necessary files needed for the HelloWorld JSF application. Put it in the Tomcat’s webapps directory. Don’t forget to restart the Tomcat server. If everything goes fine, a sample screen as follows will appear. The message for our JSF application has been highlighted.


In this example, we registered the managed bean using @ManagedBean annotation. In general, there are two ways we can register a managed bean with the JSF runtime: using faces-config.xmi file and using @ManagedBean annotation.

1. Using faces-config.xml File

JSF runtime processes a special file under application’s web-inf/ directory. The name of this file is faces-config.xml and contains configuration information of the application. We can configure managed bean in this file using <managed-bean> tag to enable the JSF framework to instantiate and store them in the appropriate scope as follows:

<?xml version=”1.0” encoding=”UTF-8”?>

<faces-config <managed-bean>

….

<managed-bean-name>helloWorld</managed-bean-name>

<managed-bean-class>ukr.HelloWorld</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

</managed-bean>

</faces-config>

This registers the bean exactly the way it was done using @ManagedBean annotation. Note that the file faces-config.xml should contain only application level configuration. So, the above code could have been placed in a separate file (say managed-beans.xml) and tell the web.xml file about its existence as:

<context-param>

<param-name>javax.faces.CONFIG_FILES</param-name>

<param-value>WEB-INF/managed-beans.xml</param-value>

</context-param>

2. Using @ManagedBean Annotation

In JSF 2.0 and higher, we can use various annotations in the Java classes for different purposes, which make your development easier and faster.

The @ManagedBean annotation in a class automatically registers that class with JSF runtime as a managed bean class. We do not need to configure it in the application configuration resource file using <managed-bean> tag. Here is an example:

@ManagedBean

public class HelloWorld {/*…*/}

This annotation may have two attributes: name and eager. The value of the name attribute is the name to be used to refer to this bean further. If name attribute is absent, the name of the bean is same as the unqualified class name with the first letter in lower case. For example, the name of the above bean will be heiioWorid. The following registers a bean with name now.

@ManagedBean(name=”now”)

class DateBean {/*…*/}

If the value of the eager attribute is true, and the scope (discussed in the next section) of the managed bean is “application”, the managed bean is instantiated when the application starts. Here is an example:

@ManagedBean(name=”now”, eager=”true”)

@ApplicationScoped

class DateBean {/*…*/}

If the value of eager attribute is unspecified or false, the default “lazy” instantiation of the managed bean occurs.

3. Scope Annotations

The scope of a managed bean tells JSF runtime when to instantiate the bean and how long the instance should be kept alive. Six kinds of scopes are defined in JSF specification and are represented by scope annotations @NoneScoped, @RequestScoped, @ViewScoped, @SessionScoped, @ApplicationScoped and @CustomScoped annotations. Here is an example:

@ManagedBean(name=”now”)

@SessionScoped

class DateBean {/*…*/}

This registers the bean whose instance is created upon the first HTTP request in a session and destroyed when the HTTP session completes. If no scope annotation is used, the @RequestScoped annotation is assumed. The function of other scope annotations is shown in Table 28.2:

4. @ManagedProperty Annotation

This annotation (together with @ManagedBean) is used to inject a value into a property. Here is an example:

@ManagedBean public class MyBean {

@ManagedProperty(value=”Hi!”)

private String message;

/*…*/

}

This sets the value of message field to “Hi”. Note that this could have been done by assigning the value “Hi” to the field directly as follows:

String message = “Hi”;

However, the value of value attribute may be an EL value expression. This means the @ManagedProperty annotation may be used to refer one bean from another. For example, the value of message property of helloWorld bean may be injected as follows:

@ManagedBean

public class MyBean {

@ManagedProperty(value=”#{helloWorld.message}”)

private String message;

/*…*/

}

Note that the scope of referenced bean (helloWorld in our case) must not be shorter that the bean making the reference (myBean in our case.). The function of @ManagedProperty annotation can be implemented using <managed-property> tag in faces-config.xml file as follows:

<managed-bean>

<managed-bean-name>myBean</managed-bean-name>

<managed-bean-class>ukr.MyBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

<managed-property>

<property-name>message</property-name>

<value>#{helloWorld.message}</value>

</managed-property>

</managed-bean>

This means a bean can be referred to from a configuration resource file. The injected bean may be any bean including the param bean that holds the parameters passed with the HTTP request message. So, the value of a bean property may be set dynamically from the HTTP request. Here is an example:

@ManagedBean public class MyBean {

@ManagedProperty(value=”#{param.msg}”)

private String message;

/*…*/

}

This sets the message property by the value of msg parameter present in the HTTP request. To verify this, use the following tag in a file ManagedPropertyDemo.xhtml.

<h:outputText value=”#{myBean.message}” />

Now, use the following URL to access this page:

http://127.0.0.1:8080/jsf/ManagedPropertyDemo.xhtml?msg=Hi

Since the scope of the param bean is request, the scope of the myBean must also be request.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

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