JSF Managed Bean XML Configuration

In this tutorial we will be creating JSF Managed Bean XML Configuration. You can configure managed beans with XML inside the WEB-INF/faces-config.xml file or if you have a large project and you want to structure your managed beans you can also put them in a separate file and then reference this file inside the /WEB-INF/web.xml file using context-parameters.

Dependencies

Here is a list of dependencies we used to successfuly start the application in tomcat 8.0.24.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.memorynotfound.jsf.managedbeans</groupId>
    <artifactId>jsf-managed-bean-xml</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>JSF - ${project.artifactId}</name>
    <url>http://memorynotfound.com</url>
    <packaging>war</packaging>

    <dependencies>
        <!-- java ee api -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- JSF api and impl -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.12</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.12</version>
        </dependency>

        <!-- JSP Standard Tag Libraries -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- servlet provided by tomcat -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

Creating the ManagedBean

There is no configuration needed in the UserBean class because we are registering the managed bean with XML.

Note:
When you create a managed bean with scope SessionScope, ViewScope, CustomScoped, ApplicationScope, short every scope bigger than RequestScope, you need to implement the java.io.Serializable class because JSF can passivate the bean to Disk if required.
package com.memorynotfound;

public class UserBean {

    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

JSF Managed Bean XML Configuration

We can create a managed bean by assigning a name, class and scope.

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

    <managed-bean>
        <managed-bean-name>userBean</managed-bean-name>
        <managed-bean-class>com.memorynotfound.UserBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>

</faces-config>

Servlet Descriptor

When you registered your beans in a separate file than faces-config.xml you need to register them in web.xml by adding a context-param named javax.faces.CONFIG_FILES.

<web-app 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-app_3_1.xsd" version="3.1">

    <display-name>JSF Managed Bean XML Configuration</display-name>

    <!-- Change to "Production" when you are ready to deploy -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/managed-beans.xml</param-value>
    </context-param>

    <!-- Welcome page -->
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

    <!-- JSF mapping -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map these files with JSF -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

</web-app>

Calling the Managed Bean

We can call the Managed Bean by referencing the managed-bean-name name. e.g.: #{userBean.firstName}

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:body>

    <h1>JSF Managed Bean XML Configuration</h1>

    <h:form>
        <h:panelGrid columns="2">
            <h:outputLabel value="First name" for="firstName"/>
            <h:inputText id="firstName" value="#{userBean.firstName}"/>

            <h:outputLabel value="Last name" for="lastName"/>
            <h:inputText id="lastName" value="#{userBean.lastName}"/>

            <h:commandButton action="result" value="send"/>
        </h:panelGrid>
    </h:form>

</h:body>
</html>

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:body>

    <h1>JSF Managed Bean XML Configuration</h1>

    First name: #{userBean.firstName}
    Last name: #{userBean.lastName}

</h:body>
</html>

Demo

URL: localhost:8080/jsf-managed-bean-xml

jsf managed bean xml configurationjsf managed bean xml configuration

References

Download

You may also like...