10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>The</strong> Example JSP Pages<br />

138<br />

This version of the Duke’s Bookstore application is organized along the Model-View-Controller<br />

(MVC) architecture. <strong>The</strong> MVC architecture is a widely used architectural approach for<br />

interactive applications that distributes functionality among application objects so as to<br />

minimize the degree of coupling between the objects. To achieve this, it divides applications<br />

into three layers: model, view, and controller. Each layer handles specific tasks and has<br />

responsibilities to the other layers:<br />

■ <strong>The</strong> model represents business data, along with business logic or operations that govern<br />

access and modification of this business data. <strong>The</strong> model notifies views when it changes and<br />

lets the view query the model about its state. It also lets the controller access application<br />

functionality encapsulated by the model. In the Duke’s Bookstore application, the shopping<br />

cart and database access object contain the business logic for the application.<br />

■ <strong>The</strong> view renders the contents of a model. It gets data from the model and specifies how that<br />

data should be presented. It updates data presentation when the model changes. A view also<br />

forwards user input to a controller. <strong>The</strong> Duke’s Bookstore JSP pages format the data stored<br />

in the session-scoped shopping cart and the page-scoped database bean.<br />

■ <strong>The</strong> controller defines application behavior. It dispatches user requests and selects views for<br />

presentation. It interprets user inputs and maps them into actions to be performed by the<br />

model. In a web application, user inputs are HTTP GET and POST requests. A controller<br />

selects the next view to display based on the user interactions and the outcome of the model<br />

operations. In the Duke’s Bookstore application, the Dispatcher servlet is the controller. It<br />

examines the request URL, creates and initializes a session-scoped <strong>Java</strong>Beans component<br />

(the shopping cart), and dispatches requests to view JSP pages.<br />

Note – When employed in a web application, the MVC architecture is often referred to as a<br />

Model-2 architecture. <strong>The</strong> bookstore example discussed in Chapter 4, “<strong>Java</strong> Servlet<br />

Technology,” which intermixes presentation and business logic, follows what is known as a<br />

Model-1 architecture. <strong>The</strong> Model-2 architecture is the recommended approach to designing<br />

web applications.<br />

In addition, this version of the application uses several custom tags from the <strong>Java</strong>Server Pages<br />

Standard Tag Library (JSTL), described in Chapter 7, “<strong>Java</strong>Server Pages Standard Tag Library”:<br />

■ c:if, c:choose, c:when, and c:otherwise for flow control<br />

■ c:set for setting scoped variables<br />

■ c:url for encoding URLs<br />

■ fmt:message, fmt:formatNumber, and fmt:formatDate for providing locale-sensitive<br />

messages, numbers, and dates<br />

Custom tags are the preferred mechanism for performing a wide variety of dynamic processing<br />

tasks, including accessing databases, using enterprise services such as email and directories, and<br />

implementing flow control. In earlier versions of JSP technology, such tasks were performed<br />

with <strong>Java</strong>Beans components in conjunction with scripting elements (discussed in Chapter 9,<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!