Controllers
Spring MVC delegates the responsibility for handling HTTP requests to Controllers. Controllers
are much like servlets, mapped to one or more URIs and built to work with HttpServletRequest
and HttpServletResponseobjects. The ControllerAPI makes no attempt to hide its dependence
to the Servlet API, instead fully embracing it and exposing its power.
Controllers are responsible for processing HTTP requests, performing whatever work
necessary, composing the response objects, and passing control back to the main request
handling work flow. The Controllerdoes not handle view rendering, focusing instead on
handling the request and response objects and delegating to the service layer.
The Controllerclass hierarchy is rich with implementations, spanning a wide range of
functionality. Some of the included Controllers are
- simple request handlers with no work flow
- form controllers handling complete the XHTML form life cycle
- wizard controllers, providing multipage work flows
•WebWork-esque one-off controllers
- flexible, multi-action controllers
Controllers are intended to be singletons, just like servlets. As singletons, they will handle
concurrent requests,^1 so they should never maintain state during a request.
Views
The response from a Controlleris rendered for output by an instance of the Viewclass. The
Controllerdoes not perform any view rendering, nor is it aware of the view rendering tech-
nology used.
Spring MVC has excellent support for many different view rendering technologies, includ-
ing template languages such as
•JSP and JSTL
•Velocity (http://jakarta.apache.org/velocity)
- FreeMarker (http://freemarker.sourceforge.net)
Views can render more than just text output. Other bundled view rendering toolkits
include
•Excel
•JasperReports (an open-source reporting tool, http://jasperreports.sourceforge.net)
52 CHAPTER 4 ■JUMP INTO SPRING MVC
- This is true, unless the Controlleris an instance of a ThrowAwayController. See Chapter 6.