Extending Spring s Handler Infrastructure


Extending Spring's Handler Infrastructure

When using Spring Web MVC in complex projects, you might run into situations where you need to extend the handler infrastructure itself. This can be done in one of two ways: by extending the Spring Controller interface (or by subclassing one of the classes implementing it), or by writing a new HandlerAdapter. This section covers the latter option. This is really an infrastructural feature of Spring MVC that is seldom used in normal applications because of the many other extension points offered by the framework. However, it's worth covering to round out your knowledge of the frame- work's architecture.

An org.springframework.web.servlet.HandlerAdapter defines exactly how, when, and if at all controllers are executed. Two HandlerAdapters exist, which are automatically supported when using the Spring DispatcherServlet. The first is the HandlerAdapter supporting the org.springframework.web.servlet.mvc controller infrastructure (all classes implementing the Controller interface). The second is the one supporting the org.springframework.web.servlet.mvc.ThrowayControllerHandlerAdapter.

The HandlerAdapter interface has the following methods:

  • boolean supports(Class clzz): Identifies if the HandlerAdapter supports this type of handler. In case of the common Spring Controller infrastructure, this class returns true for all classes implementing the org.springframework.web.servlet.mvc.Controller interface.

  • ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler): Should handle the actual request. Only called if the HandlerAdapter returned true for when it was asked if it supported the handler class (using the previously explained method). This call should return a ModelAndView or null. The latter indicates the handler has dealt with the request itself.

  • long getLastModified(HttpServletRequest request, Object handler): Should return -1 if there is no support for determining the last modification date or an appropriate long value if it does. This method is called only if the HandlerAdapter supports the Handler's class and adheres to the same contract as defined for the HttpServletRequest.getLastModified() method.

As mentioned previously, two handler adapters are installed by default: the ThrowAwayControllerHandlerAdapter and the SimpleControllerHandlerAdapter. Additional handler adapters can be installed to fully customize Spring's behavior if needed, by adding them to the WebApplicationContext. They are automatically picked up by Spring, just as with the LocaleResolver or MultipartResolver beans.

While we recommend using Spring's Web MVC default Controller infrastructure, there might be situations in which you need to customize the handling of all requests in your web application.



Professional Java Development with the Spring Framework
Professional Java Development with the Spring Framework
ISBN: 0764574833
EAN: 2147483647
Year: 2003
Pages: 188

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net