Spring MVC ControllerClassNameHandlerMapping

ControllerClassNameHandlerMapping detects all controllers in application context, remove the ‘Controller’ and uses the lowercase to create URL mapping with “/” as suffix. This implementation does not support suffixes like .htm. For Example :

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

<bean class="com.kruders.controller.HelloWorldController" />

In above code, if URI pattern /helloworld is requested, DispatcherServlet will forward the request to the “HelloWorldController“.

Following example demonstrates Spring MVC ControllerClassNameHandlerMapping .

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<properties>
    <spring.version>2.5.6</spring.version>
</properties>
 
<dependencies>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
</dependencies>

1. Controller

Now create HelloWorldController in com.kruders.controller package and write the following code.

HelloWorldController.java

package com.kruders.controller;

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 HelloWorldController extends AbstractController{
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		ModelAndView model = new ModelAndView("helloworld");
		model.addObject("message", "Hello World!!!");
		return model;
	}
}

2. Create View

Create jsp folder in WEB-INF and create helloworld.jsp file in jsp folder

Now write the following code in helloworld.jsp file

${message}

3. Configuration

Create Spring Bean Configuration in WEB-INF folder and name it dispatcher-servlet.xml and add the following code.

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

<bean class="com.kruders.controller.HelloWorldController" />
 
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
     
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

As you can see in above code, name is not required while creating HelloWorldController bean

4. Integrate Spring in Web App

To integrate Spring in Web Application, write the following code in web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
 
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
 
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Case Sensitive

You can set the “caseSensitive” property to true to apply case sensitivity to the generated paths. e.g. turning the class name “HelloWorld” into “helloWorld”.

	<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
		<property name="caseSensitive" value="true" />
	</bean>

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

HelloWorldController -> /helloWorld*

Path Prefix

You can specify a prefix to prepend to the path generated from the controller name.
Default is a plain slash (“/”). A path like “/myModule” can be specified in order to have controller path mappings prefixed with that path, e.g. “/myModule/helloworld” instead of “/helloworld” for the class name “HelloWorldController”.

	<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
		<property name="pathPrefix" value="/myModule" />
	</bean>

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

HelloWorldController -> /myModule/helloworld*

Base Package

You can set the base package to be used for generating path mappings, including all subpackages underneath this packages as path elements.

	<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
		<property name="basePackage" value="com.kruders.controller" />
	</bean>

5. Run

Now when you run the project, following screen will be displayed as in Figure 71.1

Figure 71.1 Figure 71.1

The folder structure of the example is shown below in Figure 71.2

Figure 71.2 Figure 71.2

You can download the source code of this example here.

, ,

No comments yet.

Leave a Reply