background image

Model-View-Controller Architecture

<< The Bookstore Application | Scripting in JSP Pages >>
<< The Bookstore Application | Scripting in JSP Pages >>

Model-View-Controller Architecture

This version of the Duke's Bookstore application is organized along the Model-View-Controller
(MVC) architecture. The MVC architecture is a widely used architectural approach for
interactive applications that distributes functionality among application objects so as to
minimize the degree of coupling between the objects. To achieve this, it divides applications
into three layers: model, view, and controller. Each layer handles specific tasks and has
responsibilities to the other layers:
The model represents business data, along with business logic or operations that govern
access and modification of this business data. The model notifies views when it changes and
lets the view query the model about its state. It also lets the controller access application
functionality encapsulated by the model. In the Duke's Bookstore application, the shopping
cart and database access object contain the business logic for the application.
The view renders the contents of a model. It gets data from the model and specifies how that
data should be presented. It updates data presentation when the model changes. A view also
forwards user input to a controller. The Duke's Bookstore JSP pages format the data stored
in the session-scoped shopping cart and the page-scoped database bean.
The controller defines application behavior. It dispatches user requests and selects views for
presentation. It interprets user inputs and maps them into actions to be performed by the
model. In a web application, user inputs are HTTP GET and POST requests. A controller
selects the next view to display based on the user interactions and the outcome of the model
operations. In the Duke's Bookstore application, the Dispatcher servlet is the controller. It
examines the request URL, creates and initializes a session-scoped JavaBeans component
(the shopping cart), and dispatches requests to view JSP pages.
Note ­
When employed in a web application, the MVC architecture is often referred to as a
Model-2 architecture. The bookstore example discussed in
Chapter 4, "Java Servlet
Technology,"
which intermixes presentation and business logic, follows what is known as a
Model-1 architecture. The Model-2 architecture is the recommended approach to designing
web applications.
In addition, this version of the application uses several custom tags from the JavaServer Pages
Standard Tag Library (JSTL), described in
Chapter 7, "JavaServer Pages Standard Tag Library"
:
c:if
, c:choose, c:when, and c:otherwise for flow control
c:set
for setting scoped variables
c:url
for encoding URLs
fmt:message
, fmt:formatNumber, and fmt:formatDate for providing locale-sensitive
messages, numbers, and dates
Custom tags are the preferred mechanism for performing a wide variety of dynamic processing
tasks, including accessing databases, using enterprise services such as email and directories, and
implementing flow control. In earlier versions of JSP technology, such tasks were performed
with JavaBeans components in conjunction with scripting elements (discussed in
Chapter 9,
The Example JSP Pages
The Java EE 5 Tutorial · September 2007
138