Professional Documents
Culture Documents
The Oracle Corporation Is An American Global Computer Technology
The Oracle Corporation Is An American Global Computer Technology
Design Patterns
You can also find information on these patterns by visiting the following
website, which is part of the Java Developer Connection:
http://developer.java.sun.com/developer/restricted/patterns/
J2EEPatternsAtAGlance.html
The patterns listed below are of particular relevance to the building of web
applications. More details are provided in the sections that follow:
Front Controllers
A Front Controller provides a single entry point through which requests for
several resources in a web application pass. One Front Controller might
handle all requests to the application. Several Front Controllers might
handle requests for portions of the application. Typically implemented as
servlets, Front Controllers are frequently used for the following tasks:
You can maintain and control a web application more effectively if you
funnel all client requests through a Front Controller. Functions such as view
selection, security, and template creation can be centralized in this design
pattern. The Front Controller applies these functions consistently across all
pages or views. Consequently, when the behavior of these functions needs
to change, only the Front Controller and its helper classes need to be
changed. They constitute a relatively small portion of the application.
In the two-tier form of a web application, shown in FIGURE 3-1, the
recommended approach is for the Front Controller to deal with user
requests. The Front Controller also determines which presentation element
is to be shown to users and which data is to be used for the chosen
presentation. This strategy contrasts to the traditional approach in which
each user request must be mapped to a separate view.
Note that Front Controllers do not have to route requests directly to Views.
You can chain them so that, for instance, one Front Controller accesses
user profile information. Then it could forward that profile to another Front
Controller.
View Mappers
When web resources differ based on the type of client, you can use a View
Mapper to assist the Dispatcher mechanism. Such clients could include a
web browser, personal desktop assistant, or cell phone. For instance, you
might be developing a web application that retrieves information about
waves and tides. In this situation, your users might want to view this data
from desktop personal computers or cell phones. Instead of dispatching to
a single JSP page, your web application might use the View Mapper to
send a different JSP page, depending on the type of client.
1. For example, when your web application receives incoming requests, it
routes them to a Front Controller servlet.
2. The Front Controller retrieves the appropriate data using a Helper bean.
3. It then determines the appropriate view type based on the client within
the View Mapper.
4. Based on input from the View Mapper, the Dispatcher returns the view
information to the Front Controller.
In your wave and tide application, you might not initially know whether you
wanted to display the information on a PDA or on a phone. In this case, the
View Mapper would enable you to create alternative objects or families of
objects.
Use View Mappers not only to redirect information to different devices, but
to different locales or different views.
Helpers
The Front Controller servlet can easily become quite large and unwieldy.
Therefore, use Helper classes to break out specific features and make the
application easier to build and maintain. Here are some tasks that can be
encapsulated as Helper classes:
[D]
You can implement Helpers as regular Java classes. See Using Additional
Classes or Beans for details.
Composite Views
For information on using the Forte for Java IDE to implement a Composite
View pattern, see Creating a Composite View Template.
Typically, web pages need to be reused and maintained over time. Use
View Creation Helper beans when you need to display data as it is
received. Examples of such information might be tables or sets of links. A
View Creation Helper can be any bean or Java class. However, since they
are specifically geared toward presentation in JSP pages, View Creation
Helpers are typically tag handler classes.
The View Creation Helper class provides a way to avoid placing Java code
related to specific presentation features directly in the JSP file or Front
Controller servlet. For instance, your web application might contain a
catalog search that results in certain display results. In this situation,
encapsulate the behavior into JSP tags, as shown in FIGURE 3-6:
Similarly, your web application might require that logic to format data within
its views. View Creation Helper beans can be obtained and used in the
same manner as any other beans from within a JSP file. See Using
Additional Classes or Beans for more information on using the IDE with
beans.
Model Objects
Model objects are Java objects that encapsulate application data inside a
web application. For instance, in a shopcart e-commerce application, a
model object might be an abstraction for a plush toy. Examples of the data
would include the toy's name, description, price, stock on hand, and so
forth. This information is typically retrieved from database data is inefficient.
Hence, you must design session data carefully. For additional information
on accessing databases with JDBC, see Using Java DataBase
Connectivity, a volume in the Forte for Java Programming Series.
The model object can be passed to the JSP file from a Front Controller
servlet two ways:
Frameworks
Web application frameworks typically provide support for functions such as:
Struts
Struts is an open-source framework from the Jakarta Project. It is designed
for building web applications with the Java Servlet API and JSP technology.
The Struts package provides an integrated set of reusable components.
The set includes a controller servlet, JSP custom tag libraries, and utility
classes. The components are for creating user interfaces that can be
applied to any web-based connection. Struts facilitates the standardization
of workflow and the achievement of simplicity and reusability. For more on
the Struts framework, see http://jakarta.apache.org/struts/
JATO
JavaServer Faces
JavaServer Faces proposes to define a standard set of JSP tags and Java
classes. This set aims to simplify the building of Java web application
graphical user interfaces (GUIs). JavaServer Faces defines complex HTML
forms and other common GUI elements. Furthermore, it enables tools and
third-party component vendors to focus on a single component framework
for JSP pages and servlets. It intends to bridge the gap between
conventional GUI toolkit developers and web-based GUI developers. It
hopes to provide familiar APIs for GUI components, component states, and
for rendering and input processing. Comprehensive support for
internationalization and basic input validation is proposed. This support
should ensure that developers include internationalization and input
validation in their first releases. For more information on JavaServer Faces,
see http://www.jcp.org/jsr/detail/127.jsp