Understanding HandlerMapping in SpringMVC

In the previous blog post about SpringMVC workflow, we left some questions unanswered. Today, we'll dive deeper into HandlerMapping, a crucial component in the SpringMVC framework.

What is HandlerMapping?

The HandlerMapping interface provides the getHandler(HttpServletRequest request) method, which returns a HandlerExecutionChain. This chain contains the handler object and any associated interceptors. Let's examine the logs to understand how different HandlerMapping implementations work.

The example project follows the structure from our Maven-based SpringMVC setup tutorial. The logs reveal important information about handler mappings. Specifically, two mapping strategies are visible:

  • AbstractHandlerMethodMapping maps the URL [/index/hello.do] to the com.cyw.web.controller.IndexController.getTest method
  • AbstractUrlHandlerMapping maps the URL path [/**] to org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler

These represent two different approaches to URL-to-handler mapping in SpringMVC. Various implementations of the HandlerMapping interface exist within the org.springframework.web.servlet.handler package. You can explore these by checking the API documentation or using your IDE's reference search (right-click → references → project) to understand their usage patterns.

The following class diagram illustrates the relationships between the key HandlerMapping implemetnations:

HandlerMapping Implementations

1. RequestMappingHandlerMapping

This is the most commonly used HandlerMapping in modern SpringMVC applications. As seen in the logs, it relies on AbstractHandlerMethodMapping under the hood. It enables annotation-based URL mapping, allowing you to map URLs directly to controller methods using annotations.

public ModelAndView handleTestRequest(HttpServletRequest request, HttpServletResponse response) {
    ModelAndView view = new ModelAndView("Index");
    String parameter = request.getQueryString();
    System.out.println(parameter);
    request.setAttribute("data", parameter + "abc");
    return view;
}

}


</div>### 2. BeanNameUrlHandlerMapping

This mapping stratgey maps URLs to controllers based on the bean name. The controller must extend `AbstractController`. When you configure a bean with an ID that starts with a forward slash (/), it automatically becomes accessible via that URL path.

<div>```
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean id="/hello" class="com.example.web.controller.GreetingController"></bean>

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController;

public class GreetingController extends AbstractController {

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    return new ModelAndView("Greeting");
}

}


</div>### 3. SimpleUrlHandlerMapping

This provides the most straightforward approach to URL-to-controller mapping. It supports multiple configuration styles:

**Using property key:**

<div>```
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/greeting.htm">greetingController</prop>
        </props>
    </property>
</bean>

<bean id="greetingController" class="com.example.web.controller.GreetingController" />


</div>Summary
-------

This article provided an overview of three main HandlerMapping implementations in SpringMVC. Each has its use case:

- **RequestMappingHandlerMapping** - Best for annotation-based controller development
- **BeanNameUrlHandlerMapping** - Simple approach using bean names as URL paths
- **SimpleUrlHandlerMapping** - Flexible configuration with explicit URL-to-controller mappings

Understanding how these mappings work is essential for mastering SpringMVC request handling. Further investigation into the source code will reveal the internal lookup mechanisms and help you choose the right strategy for your specific requirements.

Tags: Spring Java-EE spring-mvc handler-mapping Servlets

Posted on Sun, 13 Sep 2026 16:32:53 +0000 by greenday