ControllerClassNameHandlerMapping class for handling convention mapping

In this section, you will learn about handling mapping in convention style using ControllerClassNameHandlerMapping class.

ControllerClassNameHandlerMapping class for handling convention mapping

ControllerClassNameHandlerMapping class for handling convention mapping

In this section, you will learn about handling mapping in convention style using ControllerClassNameHandlerMapping class.

Spring 3.2 supports conventional style mapping through ControllerClassNameHandlerMapping class. You need to configure ControllerClassNameHandlerMapping class in Spring MVC configuration file. This class determines the mapping between request URLs and the Controller, which handle these requests.

The ControllerClassNameHandlerMapping discovers all the Controllers defined in application context and removes the 'Controller' from their name. The resultant name will be the request URL.

For example :

  • WelcomeController maps to the /welcome* request URL

  • HomeController maps to the /home* request URL

To understand it completely, consider a scenario :

Suppose we have a simple controller :

public class FormHandlerController implements Controller {

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
		// implementation
	}
}

The Spring MVC configuration file contains the related configuration for the above controller :

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<bean id="formHandler" class="a.b.c.FormHandlerController ">
	<!-- dependencies -->
</bean>

ControllerClassNameHandlerMapping class finds all the Controller defined in the configuration file and chops the Controller from their name to specify its handler mappings.

In the above case, the ControllerClassNameHandlerMapping class  chops the Controller from its name and define its handler mapping. So the FormHandlerController  maps to the /formhandler* request URL.

You can represent it graphically as :

FormHandlerController -------------maps to------------> /formhandler*