By Hans Bergsten
Book Price: $39.95 USD
£28.50 GBP
PDF Price: $27.99
Cover | Table of Contents | Colophon
...
<h:form>
<table>
...
<tr>
<td>Favorite Foods:</td>
<td>
<h:selectManyCheckbox value="#{cust.foodSelections}">
<f:selectItem itemValue="z" itemLabel="Pizza" />
<f:selectItem itemValue="p" itemLabel="Pasta" />
<f:selectItem itemValue="c" itemLabel="Chinese" />
</h:selectManyCheckbox>
</td>
</tr>
...
</table>
</h:form>
...<h:selectManyCheckbox> and
<f:selectItem> elements. When the form is
submitted, the JSF framework saves the list of current selections in
an application object on the server (referenced by the
#{cust.foodSelection} expression specified by the
value attribute in this example). When the
response is rendered, JSF uses the list to check off the
corresponding boxes. Separating logic from the layout in this manner
makes the page author's life easier.http://java.sun.com/j2ee/javaserverfaces/(Sun's JSF site) and http://www.jamesholmes.com/JavaServerFaces/
(an independent JSF resources site run by James Holmes). You may want
to evaluate some of these tools when you're ready to
start developing your application, but all you really need to work
with the examples in this book is a regular text editor, such as
Notepad, vi, or Emacs, and of course the Tomcat server.
Subscriber to hold the
subscriber information:package com.mycompany.newsservice.models;
public class Subscriber {
private String emailAddr;
private String[] subscriptionIds;
public String getEmailAddr( ) {
return emailAddr;
}
public void setEmailAddr(String emailAddr) {
this.emailAddr = emailAddr;
}
public String[] getSubscriptionIds( ) {
return subscriptionIds;
}
public void setSubscriptionIds(String[] subscriptionIds) {
this.subscriptionIds = subscriptionIds;
}
}Subscriber class adheres to the JavaBeans
method naming
conventions—"properties" are
defined by methods for getting their values, named
get plus the name of the property, and methods for
setting their values, named set plus the property
name. As you'll see shortly, this makes it easy to
use properties of this class as JSF UI component models.Subscriber class. To keep the
example simple, we'll add a method that represents
this behavior to the Subscriber class. Also,
instead of saving the information in a database, we just write it to
System.out: public void save( ) {
StringBuffer subscriptions = new StringBuffer( );
if (subscriptionIds != null) {
for (int i = 0; i < subscriptionIds.length; i++) {
subscriptions.append(subscriptionIds[i]).append(" ");
}
}
System.out.println("Subscriber Email Address: " + emailAddress +
"\nSubscriptions: " + subscriptions);
}
Subscriber class from
the previous section. The component writer develops the
SubscriberHandler class, as shown in this section.
All the other classes in Figure 2-2 are provided by
the JSF implementation.UIComponentBase class is the base class for
all JSF UI components. Subclasses represent specific interface
elements, such as text fields, links and buttons, labels, menus and
selection lists. JSF includes a set of component classes that can be
used right out of the box, such as the ones shown in Figure 2-2, but a component writer can also implement
custom components if needed. The UIInput class
represents an input field and lets you bind the component to an
application model through a value binding. When
a component is rendered, it pulls its value from the application
model object based on this value binding. Similarly, when an input
component processes a request, it updates the model
it's bound to with the value received with the
request. In this example, value bindings for the
UIInput and the UISelectMany
(the checkbox group) components bind the components to the
corresponding properties in the Subscriber
application class.http://java.sun.com/j2ee/javaserverfaces. For
deployment of your applications, you may want to use the JSF
implementation supported by your production server instead, if any.http://java.sun.com/j2se/. I recommend that
you download and install the Java 2 SDK (a.k.a. JDK), as opposed to
the slimmed-down Runtime Environment (JRE) distribution. The reason
is that JSP, and therefore JSF applications that use JSP, requires a
Java compiler, included in the SDK but not in the JRE.http://www10.software.ibm.com/developerworks/opensource/jikes/).
Tomcat can be configured to use Jikes instead of the
javac compiler available in the Java 2 SDK from
Sun; read the Tomcat documentation if you would like to try this. To
make things simple, though, I suggest installing the Java 2 SDK from
Sun. The examples were developed and tested with Java 2 SDK, Standard
Edition, v1.4.2. I suggest that you use the latest version of the SDK
available for your platform.C:\> echo %JAVA_HOME%
C:\jdk1.4.2C:\> set JAVA_HOME=C:\jdk1.4.2 C:\> set PATH=%JAVA_HOME%\bin;%PATH%
http://jakarta.apache.org/site/binindex.cgi.
On this page, you will find three types of builds: release builds,
milestone builds, and nightly builds. Release
builds are
stable releases that have been tested
extensively and verified to comply with the supported specifications.
Milestone
builds are
created as intermediary steps towards a release build. They often
contain new features that aren't yet fully tested
but are generally known to work. A nightly
build, however, may be very unstable.
It's actually a snapshot of the latest source code
and may have been tested only by the person who made the latest
change. You should use a nightly build only if
you're involved in the development of Tomcat.http://localhost:8080/.
localhost, you need
to adjust your proxy settings. For Netscape
and Mozilla, you find the proxy settings under Edit
→ Preferences
→ Advanced
→ Proxies, and for Internet
Explorer, you find them under Tools→
Internet Options→
Connections→
→
> LAN Settings. Make sure that the proxy isn't used
for local addresses, such as localhost and
127.0.0.1.C:\Jakarta\jakarta-tomcat-5.0.18\bin> shutdown
http://www.oreilly.com/catalog/jsvrfaces/.http://www.hansbergsten.com/
C:\JSFBook> jar xvf jsfexamples.zip
C:\JSFBook> xcopy /s /i jsfbook %CATALINA_HOME%\webapps\jsfbook
[hans@gefion jsfbook] cp -R jsfbook $CATALINA_HOME/webapps
/cover.gif /index.html /expense/reports.jsp ... /WEB-INF/web.xml /WEB-INF/classes/JSPSourceServlet.class ... /WEB-INF/lib/commons-logging.jar /WEB-INF/lib/jsf-api.jar /WEB-INF/lib/jsf-ri.jar /WEB-INF/lib/jsfbook.jar ...
javax.servlet package contains classes and
interfaces that are protocol-independent, while the
javax.servlet.http package provides HTTP-specific
extensions and utility classes.javax.servlet.http.HttpServlet
init() and destroy(
). The init() method is called once
before the servlet is asked to process its first request, and the
destroy() method is called just before the
servlet is taken out of service.doGet(
), doPost(), doDelete(
), doHead(), doOptions(
), doPut(), and doTrace(
). Most subclasses override the doGet()
and doPost() methods (or at least one of them) to
handle the corresponding HTTP request types, but rely on the default
implementations for the other request types.javax.servlet.http.HttpServletRequest
println(
) calls for adding HTML elements to the response that you
see in Example 4-1
Example 4-2
to be moved from the
servlet code to a separate file. This file can be managed by someone
who knows HTML but is not a programmer, allowing programmers to focus
on developing the application business logic instead of changing
details in the user interface look every so often. Figure 4-6 illustrates this separation of concerns on
different application component types.
javax.servlet.ServletContext,
javax.servlet.http.HttpSession, and
javax.servlet.ServletRequest.public void setAttribute(String name, Object value);
public Object getAttribute(String name);
public void removeAttribute(String name);
setAttribute() method on the
ServletContext to make the catalog available to
the servlet, and the servlet uses the getAttribute(
) method to obtain it. To make the query result available
to the JSP page, the servlet calls the setAttribute(
) method of either the HttpSession or
the ServletRequest object (depending on how long
the result must be available).
java.util.Map instances, as shown in Table 4-5. Assuming the servlet in the product catalog
example saves the query result as a bean with properties named
rowCount and rows in a request
attribute named