Processing the Form Data
ThePBuilder 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>

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
- 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.
Return to ONJava.com.
-
Great article
2008-01-07 10:49:51 misty4eyed [View]
-
Regarding the CommandButton data to Bean/database
2006-11-29 00:38:27 RAMKUMAR_C_R [View]
-
can we still have dispatcher servelet when using jsf
2004-07-09 08:48:28 hylee [View]
- Trackback from http://squizlog.keithpitty.org/archives/000169.html
JSF Resources
2004-04-28 23:03:01 [View]
-
Could you tell me how to solve the Chinese problem
2004-02-25 17:46:47 mqglt [View]
-
Good Explanation of JSF
2003-10-22 10:47:20 anonymous2 [View]
-
JSF Missing the Point
2003-10-01 08:00:02 anonymous2 [View]
-
JSF Missing the Point
2003-10-01 07:59:36 anonymous2 [View]
-
JSF Missing the Point
2003-10-01 07:59:33 anonymous2 [View]
-
JSF Missing the Point
2003-10-01 07:59:20 anonymous2 [View]
-
Echo and EchoPoint
2003-09-19 01:41:22 anonymous2 [View]
-
Front Controller
2003-09-09 11:21:27 anonymous2 [View]
-
OverKill
2003-09-03 17:55:26 anonymous2 [View]