Expert Spring MVC and Web Flow

(Dana P.) #1


  • ${special.departFrom.name} - ${special.arriveAt.name} from
    $${special.cost}



Search for a flight.




■CautionAlways place the XML declaration (<?xml?>) on the first line of your JSP, as the XML specifica-
tion requires this. If you place any JSP elements, such as the <%@ taglib %> directive on the first line, a
blank line will appear when the content is rendered to the client. This will create non-conforming XHTML.


Notice that we are iterating over a variable named specials, the same variable placed into
the model by the HomeController. There are no Spring MVC–specific elements in this JSP file,
keeping the learning curve low as you can rely on standard JSP elements and tags.
As you can see, the model object named specialswere exposed to the JSP page as request
scoped attributes for easy scripting. Each view technology is responsible for exposing and
obtaining the objects from inside the model.


View Configuration


If you recall, the HomeControllercreated a ModelAndViewwith a view name of home. How does
that logical Viewname lead to an actual Viewinstance (in this case a JSP page)? An instance of
org.springframework.web.servlet.ViewResolversteps in and provides the mappings between
Viewnames and the Viewinstance.
For our JSP files, the simple org.springframework.web.servlet.view.
InternalResourceViewResolverwill provide the perfect view resolution. In this context,
Internal Resourcerefers to any resource accessible via the Servlet API’s RequestDispatcher.
We like using the InternalResourceViewResolverfor two reasons. First, unlike other
ViewResolverimplementations, the InternalResourceViewResolverdoes not require explicit
mappings between Viewnames and Viewinstances, which significantly reduces the amount
of configuration required. This works when your logical Viewnames from the ModelAndView
match filename substrings of your JSP pages. The second reason why we like this particular
ViewResolveris that it defaults to supporting JSP pages as Views, further reducing the configu-
ration necessary.


■Tip Learn all about the many different Views andViewResolveroptions in Chapters 7 and 8.


CHAPTER 4 ■JUMP INTO SPRING MVC 57
Free download pdf