ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Using JSF
Pages: 1, 2, 3, 4

Processing the Form Data

The PBuilder class contains a static method that takes a PBean instance and builds an HTML paragraph with the properties of the JavaBean object:
package com.devsphere.articles.usingjsf;

public class PBuilder {

    public static String toHTML(PBean pbean) {
        StringBuffer buf = new StringBuffer();
        buf.append("<p align=\"");
        buf.append(pbean.getAlign());
        buf.append("\">");
        buf.append("<font size=\"");
        buf.append(pbean.getSize());
        buf.append("\" color=\"");
        buf.append(pbean.getColor());
        buf.append("\"");
        Object font[] = pbean.getFont();
        if (font != null && font.length > 0) {
            buf.append(" face=\"");
            for (int j = 0; j < font.length; j++) {
                if (j > 0)
                    buf.append(',');
                buf.append(font[j]);
            }
            buf.append("\"");
        }
        buf.append(">");
        if (pbean.isBold())
            buf.append("<b>");
        if (pbean.isItalic())
            buf.append("<i>");
        if (pbean.isUnderline())
            buf.append("<u>");
        String s = pbean.getText();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            switch (ch) {
                case '<':
                    buf.append("<");
                    break;
                case '>':
                    buf.append(">");
                    break;
                case '&':
                    buf.append("&");
                    break;
                case '"':
                    buf.append(""");
                    break;
                default:
                    buf.append(ch);
            }
        }
        if (pbean.isUnderline())
            buf.append("</u>");
        if (pbean.isItalic())
            buf.append("</i>");
        if (pbean.isBold())
            buf.append("</b>");
        buf.append("</font>");
        buf.append("</p>");
        return buf.toString();
    }

}



The view.jsp page uses <jsp:useBean> to get the JavaBean instance managed by JSF, calls the toHTML() method of PBuilder, and outputs the resultant HTML paragraph. The <h:command_hyperlink> tag of JSF is used to provide a link back to edit.jsp:

<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

<html>
<head>
<title>View</title>
</head>
<body>

<jsp:useBean id="pbean" scope="session"
    class="com.devsphere.articles.usingjsf.PBean"/>

<%= com.devsphere.articles.usingjsf.PBuilder.toHTML(pbean) %>

<f:use_faces>
    <h:command_hyperlink label="Back to Editing" href="edit.jsp"/>
</f:use_faces>

</body>
</html>

text presented by view.jsp
Figure 3. Text presented by view.jsp

Application Configuration

The JSF framework has a controller servlet named FacesServlet that must intercept the requests to any JSP page containing JSF tags. The servlet is configured in the web.xml application descriptor and is mapped to the /faces/* URL pattern. To activate the servlet, the paths of the JSP pages must be start with "faces." The index.jsp home page of the application uses response.sendRedirect() to redirect the browser to the faces/edit.jsp form:

<% response.sendRedirect("faces/edit.jsp"); %>

The path of the faces-config.xml file is specified as the value of a context parameter in the web.xml descriptor, which also contains a listener registration and a few other parameters that are specific to the reference implementation of JSF. Here is the content of web.xml:

<?xml version="1.0"?>

<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <context-param>
        <param-name>javax.faces.application.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>

    <context-param>
        <param-name>com.sun.faces.saveStateInClient</param-name>
        <param-value>false</param-value>
    </context-param>

    <context-param>
        <param-name>com.sun.faces.validateXml</param-name>
        <param-value>true</param-value>
    </context-param>

    <listener>
        <listener-class>com.sun.faces.config.ConfigListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

</web-app>

When generating a form for the first time, JSF builds a so-called component tree, an object structure containing information about the UI components, the validators registered to them, and so on. When the user submits the form data, JSF uses the information from the component tree to do some processing, such as validating the user input. By default, JSF stores the component tree as a session attribute. This works well once the application is finished. During development, however, when you make changes to the JSF form, including adding or removing UI components, JSF will throw exceptions because it doesn't discard the stale component tree when the JSP page is modified. The workaround is to switch the saveStateInClient flag declared in web.xml to true. After this change, JSF will serialize the component tree within the HTML form as a hidden field instead of storing it as a session attribute.

Summary

This article has presented the basic features of the JSF framework, showing how to build forms with the JSF tags. As any early-access package, the JSF Reference Implementation EA4 is not ready for deployment, but most of its functionality is usable. After more than two years spent in the Java Community Process, JSF is welcome; Java developers really need a standard tag library for building web user interfaces and a standard API for building custom web components.

Resources

Andrei Cioroianu is the founder of Devsphere and an author of many Java articles published by ONJava, JavaWorld, and Java Developer's Journal.


Return to ONJava.com.