Creating Portal Beans

Overview

This document explains how to create a backing bean to provide functionality from within the portal. A backing bean is written in Java and allows developers to provide functions and data accessible from within a web browser.

Java Class

A backing bean does not need a complicated structure to support access via .jsp's. All it must contain are methods that allow access to particular variables and functions to be accessed via the portal.

  //The package we define for all beans used by the portal
  package net.sf.gateway.portal.beans;

  public class MyBean {

        private int anInt;

        //Optional constructor (will be called when bean is created on server)
        //This can be used as a place to initialize variables etc.
        public MyBean(){

                anInt = 0;
        }

        //Example of a get method for .jsp's to retrieve data from bean
        public int getAnInt() {

                return anInt;
        }

        public void setAnInt(int anotherInt) {

                anInt = anotherInt;
        }

        //Example of an action .jsp's can call that does stuff and returns a result.
        //The result can be used for routing purposes discussed later on.
        public String lessThanFifty(){

                if(anInt < 500){
                        return "good";
                } else {
                        return "bad";
                }
        }
  }

Faces Configuration

With a bean created we can now define it in the MyFaces configuration. This tells MyFaces the bean's description, its name (this is how we will use it in a .jsp), the class to use for the bean, and the scop. The scop determines weather or not an instance of this bean will persist for the user's session , or if it will be built and re-built for each request (good for beans that just return data from other parts of the application). The configuration file is available in our repository as faces-config.xml

<faces-config>

...  
        <managed-bean>
                <description>An example bean</description>
                <managed-bean-name>myBean</managed-bean-name>
                <managed-bean-class>
                        net.sf.gateway.portal.beans.MyBean
                </managed-bean-class>
                <managed-bean-scope>session</managed-bean-scope>
        </managed-bean>
...

</faces-config>

Now you might have noticed that we added two Strings to return from our .lessThanFifty() method. We are going to enable some simple navigation based on that method call. Simply specify a navigation rule for each expected outcome from the method. Based on the string returned, MyFaces can route the user to the proper page.

<faces-config>

...  
        <navigation-rule>
                <from-view-id>/testMyBean.jsp</from-view-id>
                <navigation-case>
                        <from-outcome>good</from-outcome>
                        <to-view-id>/goodOutcomePage.jsp</to-view-id>
                </navigation-case>
        </navigation-rule>

        <navigation-rule>
                <from-view-id>/testMyBean.jsp</from-view-id>
                <navigation-case>
                        <from-outcome>bad</from-outcome>
                        <to-view-id>/badOutcomePage.jsp</to-view-id>
                </navigation-case>
        </navigation-rule>
...  

</faces-config>

JSP usage

Now that we have our bean we need to put it to work. From within a .jsp there are many things you can do based on the methods in your bean and what kind of data they support. This is beyond the scope of this tutorial and is documented many times over (see Google).

The following JSP code shows how you would use this example bean from within a .jsp to generate dynamic portal pages.

Note that this code is a snippet and more header information is needed in a real .jsp. Check out the source, there are many examples!

        <h:outputText value="The integer in MyBean is: #{myBean.anInt} feel free to change it below:"/>

        <h:inputText value="#{myBean.anInt}">

        <h:commandButton action="#{myBean.lessThanFifty}" value="Less than Fifty?"/>

Once the user loads the page for the first time the variable #{myBean.anInt} will evaluate to 0. As long as their session lasts, an instance of MyBean will retain whatever anInt is set to. If they click the command button, MyBean.lessThanFifty() will be called and will check the value and return a String to steer navigation. The bean will hold whatever integer value they provide in the inputText area and allow them to run the MyBean.lessThanFifty() check for the duration of the session.