Expert Spring MVC and Web Flow

(Dana P.) #1

■NoteBy default,AbstractControllerwill send the appropriate headers to disable caching.


It is important to note that these types of configurations can also be set in the bean defi-
nition of the Controller. It is not uncommon to see the above setCacheSeconds()method
replaced by a XML snippet. The choice of
configuration methods is up to you. However, we believe that the less XML in a system, the
better. For configuration elements that have almost no chance of changing, configuring them
in straight Java code feels like a better solution. We like XML for external issues such as wiring
the classes together, or for configuration elements that have a reasonable chance of changing.
The choice is yours, but whatever you choose it always helps to remain consistent.
The setFlightService()method is a clear sign that the HomeControllerwill require
Dependency Injection to obtain an instance of FlightService. Controllers are declared, con-
figured, and managed by the Spring Framework just like all other beans in the system.
Therefore, it is common and recommended to use Dependency Injection, and the full feature
set of the Spring Framework, when building and configuring Controllers.
The handleRequestInternal()method is analogous to the service()method of a servlet,
in that it accepts a HttpServletRequestand HttpServletResponsein order to perform some
work. One of the main differences, as you can see here, is that instead of returning void, this
method returns an instance of ModelAndView.
The main implication of this difference is that Controllers are not intended to render the
response. Instead, this task is delegated to other components of Spring MVC. This is a benefit
to Controllerdesign, as it can now focus on delegation to the service layer and safely ignore
response rendering issues.
The special deals are pulled from the FlightServiceand placed directly into the model
with the name specials. The view layer will use this name to locate the list of special deals.
The ModelAndViewinstance is constructed with the view name home, used to identify which
view to render for this Controller. These logical view names are used to keep the Controller
decoupled from the view technology.


Controller Configuration


Just like there is an ApplicationContextfor the FlightService, the web components have
their own ApplicationContext. The HomeControllerwill be defined and configured in a
spring-servlet.xmlfile, whose name has special meaning as we’ll see in a moment.
To make things simple, we will map the URI for the Controllerat the same time we define
the Controller. By default, Spring MVC will search for all beans configured with a name
parameter that begins with a /(forward slash) and will use those names as URI mappings. We
are mapping the HomeControllerto the URI /home(as shown in Listing 4-9), which will be pre-
fixed by the WebApplicationContextpath and the servlet mapping to form the full path to the
Controller. We will cover how a Controlleris fully mapped in more detail when we introduce
the web.xmlconfiguration (see the section “URI Mapping to Controllers” later in this chapter).


CHAPTER 4 ■JUMP INTO SPRING MVC 55
Free download pdf