Spring Book – Chapter 14 – Spring MVC and Spring Web Flow

Handler Adapters

Once the DispatcherServlet finds the appropriate HandlerMapping, the request is then forwarded to the appropriate Controller class as defined in the Spring configuration file. As explained earlier, this is achieved because Spring’s default HandlerAdapter namely org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter kicks in and forwards the request from the DispatcherServlet to appropriate Controller class. The working of HandlerAdapter can be pictorially represented as shown in Figure 14-11 below.

Working of HandlerAdapter in overall request flowin Spring MVC

Figure 14-11. Working of HandlerAdapter in overall request flowin Spring MVC

If HandlerMapping finds the Controller but not the method to be invoked, HandlerAdapter allows selecting an appropriate method in the controller as well. According to application demand if it requires special HandlerAdapter, you can implement the HandlerAdapter interface whose definition is shown in Listing 14-15 below.

Listing 14-15. HandlerAdapter interface definition

Spring has several built-in HandlerAdapter’s, some of which are as summarized as shown below:

  • springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdpater – HandlerAdapter which maps the handler methods in the controller based on HTTP paths, HTTP methods and request parameters expressed through the RequestMapping annotation discussed in earlier sections of this Chapter.
  • springframework.web.servlet.handler.SimpleServletHandlerAdapter – HandlerAdapter which calls the servlet’s “service” method for handling the passed-on request. This adapter is not activated by default and need to be specified as a bean in the Spring configuration file. After this it will automatically be applied to mapped handler beans that implement the Servlet interface.

Exception Resolvers

Execution of controller code in your application can throws various exceptions at various stages. HandlerExceptionResolver is SpringMVC’s strategy for handling controller exceptions. HandlerExceptionResolver Interface is to be implemented by objects than can resolve exceptions thrown during handler mapping or execution to error views. It prepares appropriate model and selects and error view and renders it to the user. Listing 14-16 below shows the HandlerExceptionResolver interface.

Listing 14-16. HandlerExceptionResolver interface

Spring MVC does provide built-in HandlerExceptionResolver imlemnattions which can be summarized as below:

  • springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver – resolves standard Spring exceptions and translates into corresponding HTTP status codes. This exception resolver is enabled by default in the DispatcherServlet.
  • springframework.web.servlet.handler.SimpleMappingExceptionResolver – resolves exceptions and maps exception class to view names. The views will have a model attribute “exception” filled in accordingly. Listing 14-17 below shows configuring SimpleMappingExceptionResolver.

Listing 14-17. Configuring SimpleMappingExceptionResolver in Spring configuration file

Page Visitors: 8091

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *