Expert Spring MVC and Web Flow

(Dana P.) #1

The View Layer


This chapter discusses the user interface layer, or view layer, of your Web MVC application.
We investigate what the goal of a view is, why it should be considered separately in an applica-
tion, and how Spring’s view layer architecture helps you achieve the goal of producing a user
interface independent of the Modeland the Controllers.
We will take a detailed tour of the mechanisms used by Spring to manage views, and we’ll
explore the benefits to be gained from this framework design. We’ll cover Views, ViewResolvers,
and their relationship to Models and Controllers.


What’s in a View


Chapter 4 introduced views on the whistle-stop tour of the sample MVC application. It’s
important to be familiar with those concepts introduced, because in this chapter we’re going
to find out what really makes them “tick” and how they interact with all the other parts of an
MVC application.
A view serves a dual purpose in a Web MVC application. Primarily, a view is responsible
for the display of a model that has been generated by a Controller. Additionally, views may
also present the user with suitable options for continued interaction with the application.
In an MVC application, it’s often useful to think of the model as the contract between the
Controllerand the View: the Viewcan only see what the Controllerpasses it via the model.
When Controllercomponents generate a model ready for a view to display, that model
should be complete. The view concentrates only on displaying the model and on presenting
the options your users can choose next. In normal circumstances, the view should never need
to call back into any Controllercode, access domain logic, or perform data retrieval functions.


■NoteYou may have come across OpenSessionInView,a common counterexample to this maxim. A
view may need to retrieve additional pages from a data store after initially rendering, rather than slowing
down the application by having all of the data loaded prior to displaying the first page. Here, the data store
session remains open while the view is rendered.


201

CHAPTER 7


■ ■ ■

Free download pdf