×
☰ See All Chapters

ControllerClassNameHandlerMapping

The convention for ControllerClassNameHandlerMapping is to take the short name of the controller class, remove the “Controller” suffix if it exists and retain the remaining text, lowercased, as the mapping, with a leading /. For example, if the controller class name is com.java4coding.controller.StudentController then the path is /student*.

By default, Spring MVC is using the BeanNameUrlHandlerMapping handler mapping.

<beans>

… …

<bean name="/welcome.htm" class="com.java4coding.controller.WelcomeController" />

<bean name="/helloGuest.htm" class="com.java4coding.controller.HelloWorldController" />

</beans>

To enable the ControllerClassNameHandlerMapping, declared it in the bean configuration file, and now the controller’s bean’s name is no longer required.

<beans>

… …

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

<bean name="/welcome.htm" class="com.java4coding.controller.WelcomeController" />

<bean name="/helloGuest.htm" class="com.java4coding.controller.HelloWorldController" />

</beans>

Now, Spring MVC is mapping the requested URL by following conventions:

WelcomeController -> /welcome*

HelloWorldController -> /helloworld*

**************************************

>>  /welcome.htm –> WelcomeController.

>> /welcomeHome.htm –> WelcomeController.

>> /helloworld.htm –> HelloWorldController.

>> / helloworld999.htm –> HelloWorldController.

>> /helloWorld.htm, failed to map to / helloworld *,  the “W” uppercase is not matched.

To solve the above case sensitive problem, we can set “caseSensitive” property to true as below:

      <beans>

             … …

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

                        <property name="caseSensitive" value="true" />

                </bean>

 

                <bean name="/welcome.htm" class="com.java4coding.controller.WelcomeController" />

                <bean name="/helloGuest.htm" class="com.java4coding.controller.HelloGuestController" />

        </beans>

 


All Chapters
Author