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:
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:
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
Article source code
JavaServer Faces Technology
Java Web Services Developer Pack
Struts-Faces Integration Library
Andrei Cioroianu
is the founder of Devsphere and an author of many Java articles published by ONJava, JavaWorld, and Java Developer's Journal.