April 22, 2019
Hot Topics:

Spring into JavaServer Faces

  • April 27, 2006
  • By Michael Klaene
  • Send Email »
  • More Articles »

Managed-Bean Accessible to a JSF Web Page


JSF Web Page Accessing a ShowRooms Instance and Its Rooms Property

      <meta http-equiv="Content-Type" content="text/html;
      <link href="styles.css" type="text/css" rel="stylesheet">
      <title>Hotel Reservation System</title>
         <h1><h:outputText value="Hotel Rooms" /></h1>
         <h:dataTable value="#{showRooms.rooms}" var="rowRoom" 
                      border="1" headerClass="Heading"
            <f:facet name="header">
               <f:verbatim>Room #</f:verbatim>
            <h:outputText value="#{rowRoom.roomNumber}"/>



ShowRooms.jsp displays a collection of Room objects. These objects should be obtained by invoking the Spring business method getRooms(). Now, look at what it will take to make this possible.

The key component that makes Spring objects available to JSF managed beans is Spring's DelegatingVariableResolver. A JSF VariableResolver can be used to bind values from objects available in one of an application's scopes, such as the request scope or session scope. JSF's VariableResolver searches through each possible scope and if it finds no match, it then searches for the value using any variable resolver specified in the faces-config file. By specifying Spring's DelegatingVariableResolver, you can inject the HotelManager instance into your JSF managed-bean:

<!-- Allows you to inject Spring beans into JSF managed beans... -->

Once this entry has been added, you must modify your managed-bean definition and define a managed-property. This property is called 'manager', the name of your Spring bean defined in applicationContext.xml:

<!--Managed-bean accessible to JSF Web page.-->

Finally, after adding the manager property to the JSF managed-bean, you can delegate to the Spring manager in the ShowRooms managed-bean:

public class ShowRooms {

   /** Creates a new instance of ShowRooms */
   public ShowRooms() {

    * Holds value of property hotelReservation.
   private HotelManager manager;

    * Setter for property hotelManager.
    * @param hotelManager New value of property hotelManager.
   public void setManager(HotelManager manager) {
      this.manager = manager;

   public HotelManager getManager() {
      return manager;

    * Returns List of all hotel rooms objects.
   public List getRooms() {
      return this.getManager().getRooms();


ShowRooms.jsp utilizes a JSF table component to display the list of Room objects using the expression "#{showRooms.rooms}". At runtime, it invokes the getRooms method to retrieve this data. Each column within the table contains output text to display a property within the current Room instance. Again, this is accomplished with expressions such as "#{rowRoom.roomNumber}". Applying a simple stylesheet, the following output appears on the page:

More complex JSF components exist to display tabular data that support sorting and pagination. This simple table is adequate for this demonstration. One area you should improve upon is the display of room data. To begin with, room id is a unique identifier with no business meaning. It is unlikely the user will care about this attribute, so you can remove it. Also, chances are good that database character codes such as 'O' for an occupied room or 'V' for a vacancy will mean little to users. One way to translate this text is to perform decodes in the database query. However, it is also possible to translate these values in the view layer. You could accomplish this by directing some of the JSF table's columns to managed-bean methods that will perform these translations. An enhanced ShowRoom bean contains the following translation methods:

//Get current room object.
private Room getCurrentRoom() {
   return (Room) FacesContext.getCurrentInstance().

//Get translated room type.
public String getDisplayedRoomType() {
   return getCurrentRoom().getRoomType().
      equals(new Character('S')) ? "Single" : "Double";

//Get translated reserve status.
public String getDisplayedReserveStatus() {
   return getCurrentRoom().getReserveStatus().
      equals(new Character('V')) ? "Vacant" : "Occupied";

//Get translated can smoke.
public String getDisplayedCanSmoke() {
   return getCurrentRoom().getCanSmoke().
      equals(new Boolean(true)) ? "Yes" : "No";

An updated ShowRoom.jsp file will reference these new methods. For example, instead of the expression "#{rowRoom.canSmoke}", use '"#{showRooms.displayedCanSmoke}"'. The result is a more meaningful display of user data:


Spring is a powerful framework for building enterprise Java applications. JSF is a standards-based technology that can simplify Web development. It is possible to combine the two with surprisingly little effort, allowing developers to take advantage of the best that both Spring and JSF have to offer. The source code for this article is available for download here.

About the Author

Michael Klaene is a Senior Consultant with Sogeti LLC. He has spent over 9 years in Information Technology and is an experienced Systems Analyst, delivering solutions that involve numerous technologies, such as J2EE and .NET.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date