Expert Spring MVC and Web Flow

(Dana P.) #1

Listing 7-8.The ViewResolver Interface


public interface ViewResolver {
View resolveViewName(String viewName, Locale locale)
throws Exception;
}


Consistent with many other key Spring interfaces, ViewResolverdefines a single method
for implementation keeping it focused on one particular task.
It should be fairly clear from the interface definition how ViewResolvers act as the key
link decoupling Controllerand view.


Putting View Resolution in Context


We’ve covered much of the detail now of how Spring manages the view tier, decouples views
from Controllers and operates a sophisticated view resolution strategy. What’s missing is an
overview of how these objects combine in your Web MVC application—what is responsible
for linking them all together?
Let’s briefly take a step back up the chain of events and examine a simplified overview of
the request/response sequence, noting where the Viewand ViewResolverfit in. Figure 7-4
shows a sequence in which the Controllerreturns a ModelAndViewinstance containing the
name of a view, rather than a concrete Viewimplementation.


Figure 7-4.Sequence diagram of named view resolution


In Figure 7-4 we can see clearly how the view resolution and rendering fits in with the
DispatcherServlet, which manages the entire operation, and the Controllerthat built our
model. The Controllerplays no part in resolving the view in this sequence and is entirely
oblivious of the operation.


: DispatcherServlet

: handleRequest(request : , response : ) : ModelAndView

: resolveViewName(viewName : String, locale : Locale) : View

: render(model : Map, request : HttpServletRequest, response : HttpServletResponse)

: Controller

: create() : ModelAndView

: ViewResolver : View

CHAPTER 7 ■THE VIEW LAYER 209
Free download pdf