Java Reference
In-Depth Information
WHAT IS THE MVC DESIGN PATTERN?
In MVC, the model represents the application's data and related business logic. The model may be
l
represented by one object or a complex graph of related objects. In a Java EE application, the data
is encapsulated in domain objects often deployed in an EJB module. Data is transported to and
from the database access layer in data transfer objects (DTOs) and accessed via data access objects
(DAOs). See Chapter 12, “Data Access Pattern.”
The view is the visual representation of the data contained in the model. A subset of the model is
represented in a single view; thus, the view acts as a i lter to the model data. The user interacts with
the model data via the view's visual representation and invokes business logic that in turn acts upon
the model data.
The controller links the view to the model and directs application l ow. It chooses which view
to render to the user in response to the user's input and the business logic that's processed. The
controller receives a message from the view, which it forwards to the model. The model in turn
prepares a response and sends it back to the controller where the view is chosen and sent to the user.
The MVC pattern logically spans the client and middle tier of a multitier architecture. In a Java
EE environment, the model is located in the business layer, normally in the form of an Enterprise
JavaBeans (EJB) module. The controller and view are located in the web tier. The view is likely to
be constructed from JavaServer Faces (JSF) or JavaServer Pages (JSP) with the help of Expression
Language (EL). The controller is normally a servlet that receives Hypertext Transfer Protocol
(HTTP) requests from the user. See Chapter
2, “Basics of Java EE” for a discussion of multitier
architecture and the different application layers.
Often the MVC pattern is combined with other patterns, such as the command (or action), strategy,
composite, and observer patterns. This chapter doesn't delve into the depths of these patterns, but it
does touch on the action pattern in the example that follows.
BACKGROUND
The i rst mention of this pattern occurred before the invention of the modern‐day
Internet in a paper published by the SmallTalk programmer Trygve Reenskaug in
December 1979 while he was working at Xerox Parc. 1
Even though the MVC elements of this pattern
were dei ned more than 30 years ago, they are
surprisingly relevant to their current use in a web
application.
Model
Update Model
Update View
Figure 14-1 shows the user making a request to
the controller. The controller handles the request
by updating the model and rendering a new view,
which is then sent to the user.
Call View
Controller
View
User Request
FIGURE 14-1: Diagram of the model view
controller pattern
 
Search WWH ::




Custom Search