JavaServer Faces Part 1 - Introduction
This is first in the series of blogs on JSF.
JSF = JavaServer Faces.
It’s a web framework. The 3 independent elements that make up a usable JSF component in a page are:
- UIComponent class – defines behavior of component. Eg. UISelectOne
- Renderer class – provides specific renderings of component. For eg, a UISelectOne can be rendered in HTML as either a group of radio buttons or a select menu.
- A JSP tag – which associates a Renderer with a UIComponent and makes them usable in JSP as a single tag, eg <h:selectOneMenu>
JSF UI components are bound to server-side Java beans (which are registered as Managed Beans in faces-config.xml). In the JSP pages, the UI components are bound to Managed Beans using the JSF Expression Language (which in JSF 1.2 is same as JSTL 2.1’s EL and is now called Unified EL). Once bound, updating bean properties or invoking bean methods from a web interface is handled automatically by JSF request processing lifecycle. This ability to automatically synchronize server-side Java Bean properties to a hierarchical set of components that are based on UI presented to the client user is a major advantage of JSF over other web frameworks like Struts.
JSF Request Processing Lifecycle
- When a JSP page with JSF components is requested first time, then JSF runtime creates an in-memory components tree on server side.
- In between requests, when nothing is happening in application, the component tree is cached on server.
- Upon a subsequent request, the component tree is reconstituted, and if form input values are sent in request, they are processed and validations are executed.
- Upon successful validation, server-side managed bean properties are updated.
- Once all event processing and updates are over, the response is sent to client.
To enable JSF support in a Java EE web application, following needs to be done:
- An entry for Faces Servlet in web.xml and mapping of this servlet to *.faces or /faces/* etc. (A request that uses the appropriate faces URL pattern can be considered a faces request and when received by faces controller, it processes the request by preparing an object known as the JSF context, which contains all accessible application data and routes the client to appropriate view page based on the navigation rules as defined in the faces-config.xml.)
- A JSF configuration file – faces-config.xml in WEB-INF/ path.
- Following jar files in WEB-INF/lib path:
- JSF jars – jsf-api.jar and jsf-impl.jar
- Apache commons jars – commons-beanutils.jar, commons-collection.jar, commons-digester.jar, and commons-logging.jar.
- JSTL jars – standard.jar and jstl.jar
For a JSP page to be JSF enabled,
- we need to include at least the following taglibs from Sun’s JSF RI (you may also use Apache MyFaces implementation of JSF spec):
- In the JSP page body, we must add <f:view> tag which becomes the base UI component of component tree in memory on server side when the page is requested for viewing.
- If page processes form input, then we can add <h:form> tag.
inputname.jsp – shows a form to user to enter name
If outcome is “greeting” then show greeting.jsp to user
The input name between the two pages is stored in memory in PersonBean’s personName field. The personName is registered as managed bean and JSF’s EL is used in the JSP pages to access the PersonBean’s personName field values.
Where, message bundle is defined in a message.properties file (which needs to be put in WEB-INF/classes path in your web applications WAR) as,
We bind a PersonBean to the inputText filed in helloForm. To do so we also need to register the PersonBean as managed bean in faces-config.xml. We also need to define the navigation rule from :
And here’s what the greeting.jsp is:
And the managed bean PersonBean.java:
This completes the short introduction to JSF 1.1.