Adding JSF components

Recommended by 0 users

Adding JSF components 

For the purpose of learning the packaging of a Java EE 6 application, we will show how to combine some JSF components, such as Managed Beans and JSF views, with some Enterprise components, such as the EJB singleton, which we formerly introduced in Chapter 3, Configuring Enterprise Services.

In this example, we will create a simple caching system that uses an EJB singleton to handle the cache in memory. Later, we will show how to introduce data persistence to keep our cache on a storage.

Let’s start by adding a page named home.xhtml to your Dynamic Web Project:

<!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”xmlns:c=”http://java.sun.com/jsp/jstl/core”>


<style type=”text/css”>

<!– omitted for the sake of brevity –>




<h2>JSF 2 example on JBoss 7</h2>

<h:form id=”jsfexample”>

<h:panelGrid columns=”2″ styleClass=”default”>

<h:outputText value=”Enter key:” />

<h:inputText value=”#{manager.key}” />

<h:outputText value=”Enter value:” />

<h:inputText value=”#{manager.value}” />

<h:commandButton actionListener=”#{manager.save}”styleClass=”buttons” value=”Save key/value” />

<h:commandButton actionListener=”#{manager.clear}”styleClass=”buttons” value=”Clear cache” />

<h:messages />


<h:dataTable value=”#{manager.cacheList}” var=”item”styleClass=”table” headerClass=”table-header”rowClasses=”table-odd-row,table-even-row”>


<f:facet name=”header”>Key</f:facet>

<h:outputText value=”#{item.key}” />



<f:facet name=”header”>Value</f:facet>

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






This page references a Managed Bean named “manager “, which is used to store and retrieve key/value pairs. Managed Beans are simple Java classes, which are used as models for UI components. In JSF 1.2, for a bean to become a managed bean, you had to register it in the JSF configuration file (faces-config.xml). One of the biggest annoyances was that as the number of beans grew, the JSF configuration file grew as well, and it was difficult to keep track of all names and changes in three different files that were all “connected” (JSF configuration file, the JSF view, and the bean itself).

Luckily, JSF 2 has introduced annotations to register Managed Beans. With annotations, the bean and its registration are in the same place (Java class), so it becomes much easier to manage.

Now, let’s see how to code the PropertyManager Managed Bean:

package com.packtpub.chapter4.bean;

import java.util.ArrayList;

import java.util.List;

import javax.ejb.EJB;

import javax.faces.application.FacesMessage;

import javax.faces.bean.ManagedBean;

import javax.faces.context.FacesContext;

import javax.faces.event.ActionEvent;

import com.packtpub.chapter4.ejb.SingletonBean;

@ManagedBean(name=”manager”) public class PropertyManager {   @EJB   SingletonBean ejb;

ArrayList  cacheList  = new ArrayList ();

private String key;

private String value;

// key/value GETTERS /SETTERS omitted for brevity   public void save(ActionEvent e) {

ejb.put(key, value);


public void clear(ActionEvent e) {

System.out.println(“Called clear”);

ejb.delete();   }

public List getCacheList(){

return ejb.getCache();



The most relevant part of this class is the @ManagedBean annotation that registers this class as a JSF Managed Bean. Next, the @EJB annotation that is used to inject the SingletonBean into the class.

Adding the EJB layer

The SingletonBean is an EJB, which is marked with the special @javax.ejb.Singleton annotation. A class with such annotation is guaranteed to be instantiated only once, thus it is the middleware equivalent of the J2SE singleton pattern. In the Java EE context, they are primarily used to store application-wide shared data.

Create a new class named SingletonBean. The aim of this class will be to store key-value pairs in HashMap:

package com.packtpub.chapter4.ejb;

import java.util.ArrayList;

import java.util.List; import javax.annotation.PostConstruct;

import javax.ejb.Singleton;

import com.packtpub.chapter4.entity.Property;


public class SingletonBean {

private  List<Property> cache;


public void initCache(){

this.cache = new ArrayList();

public void delete(){



public void put(String key,String value){

Property p = new Property();





public List<Property> getCache() {

return cache;



The last class we need to add is Property, which is a plain JavaBean class. The aim of this class will be to store key-value pairs in HashMap: package com.packtpub.chapter4.entity;

public class Property {

private String key;

private String value;

// GETTERS / SETTERS omitted for brevity


This class is intentionally placed in a package com.packtpub.chapter4.entity, since in the next section of this chapter, we will use this class as an Entity for storing data.

If you have completed all these steps correctly, you will end up with a project containing the following items:


Choosing the web context of the application

By default, a Web application inherits the Web context name from the archive name, which is deployed on the application server. So, in our example, if we deploy an archive named as7project.war, it will be accessible using the Web context name as7project, as shown by the following image:


The context name is, however, customizable, and the simplest way to achieve it (without changing the archive name) is by adding a jboss-web.xml file to the WEB-INF folder of your project:

The content of this file will include the custom Web context, as specified by the context-root element:




0 Responses on Adding JSF components"

Leave a Message

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

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.