Nearby lessons

25 of 35

Spring - HandlerMappings

📌 What You Will Learn
  • Explain the job a HandlerMapping does inside the front controller
  • Map a URL by bean name, by explicit list, by class name and by annotation
  • Choose a mapping strategy and know what each one costs you

How Spring MVC decides which controller handles a request. All four handler mappings with working examples: BeanNameUrlHandlerMapping, SimpleUrlHandlerMapping, ControllerClassNameHandlerMapping and DefaultAnnotationHandlerMapping.

HandlerMappings

 HandlerMapping is a component in Spring Web MVC Framework, it will take the incoming request URI and it will identify the mapped Controller class through Spring configuration file and return that Controller class details to DispatcherServlet.

 Spring Web MVC has provided a set of predefined HandlerMappings in the package like "org.springframework.web.servlet.handler".

 Spring Web MVC has provided the following predefined HandlerMapping classes.

  • BeanNameUrlHandlerMapping
  • SimpleUrlHandlerMapping
  • ControllerClassNameHandlerMapping
  • DefaultAnnotationHandlerMapping

BeanNameUrlHandlerMapping — ds-servlet.xml

 It is the default HandlerMapping in Spring web MVC applications if we use XML configurations, it is not required to configure in Spring configuration file.

 BeanNameUrlHandlerMapping is able to identify the Controller classes by mapping the incoming request URI with the Bean identity["id" attribute value] or Bean logical name[ "name" attribute value] which we defined in Spring configuration file.

EX:

Example02
JCode Cell
1 
2<beans>
3 -----
4 <bean name="handlerMapping"
5 
6class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
7 <bean name="/welcome" class="com.durgasoft.controller.WelcomeController"/>
8 ------
9 
10</beans>
11

BeanNameUrlHandlerMapping — index.jsp

If we submit request with the URI "welcome" then BeannameUrlHandlerMapping is able to map "welcome" with "name" attribute value in <bean> tag , if it is matched then BeanNameUrlHandlerMapping will confirm the respective Controller class is "com.durgasoft.controller.WelcomeController" and send its details to DispatcherServlet.

EX: http://localhost:1010/app/welcome

Example:

Example03
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<jsp:forward page="helloform"/>
12</body>
13</html>
14

BeanNameUrlHandlerMapping — helloform.jsp

Example04
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: red;" align="center"> Durga Software Solutions </h2>
12<h3 style="color: blue;" align="center"> User Hello Form </h3>
13<form method="POST" action="wish">
14<center>
15<table>
16<tr>
17<td>User Name</td>
18<td><input type="text" name="uname"/></td>
19</tr>
20<tr>
21<td><input type="submit" value="SayHello"/></td>
22</tr>
23</table>
24</center>
25</form>
26</body>
27</html>
28

BeanNameUrlHandlerMapping — wish.jsp

Example05
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: red;" align="center"> Durga Software Solutions </h2>
12<h3 style="color: blue;" align="center"> User Hello Status </h3>
13<h1 style="color: red;" align="center"> Hello ${uname} </h1>
14</body>
15</html>
16

BeanNameUrlHandlerMapping — HelloFormController.java

Example06
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7 
8import org.springframework.web.servlet.ModelAndView;
9import org.springframework.web.servlet.mvc.Controller;
10 
11public class HelloFormController implements Controller {
12 
13@Override
14public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
15return new ModelAndView("helloform");
16}
17}
18

BeanNameUrlHandlerMapping — WishController.java

Example07
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7import org.springframework.web.servlet.ModelAndView;
8import org.springframework.web.servlet.mvc.Controller;
9 
10public class WishController implements Controller {
11 
12@Override
13public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
14String uname = request.getParameter("uname");
15return new ModelAndView("wish", "uname", uname);
16}
17}
18

BeanNameUrlHandlerMapping — ds-servlet.xml

Example08
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:p="http://www.springframework.org/schema/p"
6xmlns:context="http://www.springframework.org/schema/context"
7xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context.xsd">
12 
13<bean name="/helloform" class="com.durgasoft.controller.HelloFormController"/>
14<bean name="/wish" class="com.durgasoft.controller.WishController"/>
15 
16<!--<bean name="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> -->
17 
18<bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
19<property name="prefix" value="/WEB-INF/"/>
20<property name="suffix" value=".jsp"/>
21</bean>
22</beans>
23

BeanNameUrlHandlerMapping — web.xml

Example09
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
4<display-name>app17</display-name>
5<welcome-file-list>
6<welcome-file>index.html</welcome-file>
7<welcome-file>index.htm</welcome-file>
8<welcome-file>index.jsp</welcome-file>
9<welcome-file>default.html</welcome-file>
10<welcome-file>default.htm</welcome-file>
11<welcome-file>default.jsp</welcome-file>
12</welcome-file-list>
13<servlet>
14<servlet-name>ds</servlet-name>
15<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16<load-on-startup>1</load-on-startup>
17</servlet>
18<servlet-mapping>
19<servlet-name>ds</servlet-name>
20<url-pattern>/</url-pattern>
21</servlet-mapping>
22</web-app>
23

SimpleUrlHandlerMapping

org.springframework.web.servlet.handler.SimpleUrlHandlerMapping is able to map the in coming request with a particular Controller class and it will provide the respective Controller class details to DispatcherServlet with the following actions.

  • SimpleUrlHandlerMapping will take incoming request URI value.
  • It will search for the Request URI name in its Properties which are configured in Spring

Configuration file.

  • If any property key is matched with incoming request URI value then

SimpleUrlHandlerMapping will take the corresponding property value , that is, logical name of the Controller class.

  • SimpleUrlHandlerMapping will identify the controller class on the basis of logical name which we get from the matched property.
Example10
JCode Cell
1 
2<beans>
3-----
4<bean name="wishController" class="com.durgasoft.controller.WishController"/>
5<bean name="welcomeController" class="com.durgasoft.controller.WelcomeController"/>
6<bean name="handlerMapping" class="org.....handler.SimpleUrlHandlerMapping>
7<property name="mappings">
8 <props>
9 <prop key="/wish">wishController</prop>
10 <prop key="/welcome">welcomeController</prop>
11 </props>
12</property>
13</bean>
14-----
15</beans>
16

SimpleUrlHandlerMapping — index.jsp

From the above example, if we submit request with the URI "wish" then SimpleUrlHandlerMapping will map this URI with properties names configured in Spring configuration file and it will identify "wishController" as value and it will identify the respective controller class like "com.durgasoft.controllers.WishController" from Spring configuration file.

EX:

http://localhost:1010/app/wish ----> wishController ---> WishController http://localhost:1010/app/welcome ---> welcomeController --> WelcomeController

Example:

Example11
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<jsp:forward page="hello"/>
12</body>
13</html>
14

SimpleUrlHandlerMapping — helloform.jsp

Example12
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: red;" align="center"> Durga Software Solutions </h2>
12<h3 style="color: blue;" align="center"> User Hello Form </h3>
13<form method="POST" action="wish">
14<center>
15<table>
16<tr>
17<td>User Name</td>
18<td><input type="text" name="uname"/></td>
19</tr>
20<tr>
21<td><input type="submit" value="SayHello"/></td>
22</tr>
23</table>
24</center>
25</form>
26</body>
27</html>
28

SimpleUrlHandlerMapping — wish.jsp

Example13
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: red;" align="center"> Durga Software Solutions </h2>
12<h3 style="color: blue;" align="center"> User Hello Status </h3>
13<h1 style="color: red;" align="center"> Hello ${uname} </h1>
14</body>
15</html>
16

SimpleUrlHandlerMapping — HelloFormController.java

Example14
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7 
8import org.springframework.web.servlet.ModelAndView;
9 
10import org.springframework.web.servlet.mvc.Controller;
11 
12public class HelloFormController implements Controller {
13 
14@Override
15public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
16return new ModelAndView("helloform");
17}
18}
19

SimpleUrlHandlerMapping — WishController.java

Example15
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7import org.springframework.web.servlet.ModelAndView;
8import org.springframework.web.servlet.mvc.Controller;
9 
10public class WishController implements Controller {
11 
12@Override
13public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
14String uname = request.getParameter("uname");
15return new ModelAndView("wish", "uname", uname);
16}
17}
18

SimpleUrlHandlerMapping — ds-servlet.xml

Example16
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:p="http://www.springframework.org/schema/p"
6xmlns:context="http://www.springframework.org/schema/context"
7xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context.xsd">
12 
13<bean name="helloFormController" class="com.durgasoft.controller.HelloFormController"/>
14<bean name="wishController" class="com.durgasoft.controller.WishController"/>
15 
16<!--<bean name="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> -->
17<bean name="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
18<property name="mappings">
19 <props>
20 <prop key="/hello">helloFormController</prop>
21 <prop key="/wish">wishController</prop>
22 </props>
23</property>
24</bean>
25<bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
26<property name="prefix" value="/WEB-INF/"/>
27<property name="suffix" value=".jsp"/>
28</bean>
29</beans>
30

SimpleUrlHandlerMapping — web.xml

Example17
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
4<display-name>app17</display-name>
5<welcome-file-list>
6<welcome-file>index.html</welcome-file>
7<welcome-file>index.htm</welcome-file>
8<welcome-file>index.jsp</welcome-file>
9<welcome-file>default.html</welcome-file>
10<welcome-file>default.htm</welcome-file>
11<welcome-file>default.jsp</welcome-file>
12</welcome-file-list>
13<servlet>
14<servlet-name>ds</servlet-name>
15<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16<load-on-startup>1</load-on-startup>
17</servlet>
18<servlet-mapping>
19<servlet-name>ds</servlet-name>
20<url-pattern>/</url-pattern>
21</servlet-mapping>
22</web-app>
23

ControllerClassHandlerMapping — index.jsp

org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping is able to map the incoming request URI with the Controller class by following the below convention.

  • It will take request URI value and removes extension if any extension.
  • It will make the first letter as Upper Case letter in the Request URI value.
  • It will append Controller to the modified Request URI value.
  • It will search for the respective controller class with the above convention and sends that

Controller class details to DispatcherServlet.

If we submit a request with the URI value "wish" then ControllerClassNameHandlerMapping will search for the Controller class with the name "WishController", in this context, "name" attribute must not be provided in the Controller class configuration .

EX:

<beans ...> <bean name="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean class="com.durgasoft.controller.HelloController" /> <bean class="com.durgasoft.controller.WelcomeController" /> </beans>

In the above example, if we submit request with the Request URI "wish" then WishContrlller class will be mapped.

If we submit request with the URI "welcome" then WelcomeController class will be mapped.

EX:

helloform ---->HelloFormController --> Valid helloForm ---->HelloFormController --> Invalid, where 'F' is not matched.

If we want to map the request URI with the Contrlller class names in case insensitive manner then we have to use "caseSensitive" property with the value "true".

EX:

<beans ...> <bean name="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="caseSensitive" value="true"/> <bean class="com.durgasoft.controller.HelloController" /> <bean class="com.durgasoft.controller.WelcomeController" /> </beans>

EX:

helloform ---->HelloFormController --> Invalid, 'f' is not matched. helloForm ---->HelloFormController --> valid.

EXAMPLE:

Example18
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<jsp:forward page="helloForm"/>
12</body>
13</html>
14

ControllerClassHandlerMapping — helloform.jsp

Example19
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: red;" align="center"> Durga Software Solutions </h2>
12<h3 style="color: blue;" align="center"> User Hello Form </h3>
13<form method="POST" action="wish">
14<center>
15<table>
16<tr>
17<td>User Name</td>
18<td><input type="text" name="uname"/></td>
19</tr>
20<tr>
21<td><input type="submit" value="SayHello"/></td>
22</tr>
23</table>
24</center>
25</form>
26</body>
27</html>
28

ControllerClassHandlerMapping — wish.jsp

Example20
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: red;" align="center"> Durga Software Solutions </h2>
12<h3 style="color: blue;" align="center"> User Hello Status </h3>
13<h1 style="color: red;" align="center"> Hello ${uname} </h1>
14</body>
15</html>
16

ControllerClassHandlerMapping — HelloFormController.java

Example21
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7 
8import org.springframework.web.servlet.ModelAndView;
9 
10import org.springframework.web.servlet.mvc.Controller;
11public class HelloFormController implements Controller {
12 
13@Override
14public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
15 
16return new ModelAndView("helloform");
17}
18}
19

ControllerClassHandlerMapping — WishController.java

Example22
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7import org.springframework.web.servlet.ModelAndView;
8import org.springframework.web.servlet.mvc.Controller;
9 
10public class WishController implements Controller {
11 
12@Override
13public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
14String uname = request.getParameter("uname");
15return new ModelAndView("wish", "uname", uname);
16}
17}
18

ControllerClassHandlerMapping — ds-servlet.xml

Example23
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:p="http://www.springframework.org/schema/p"
6xmlns:context="http://www.springframework.org/schema/context"
7xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context.xsd">
12 
13<bean class="com.durgasoft.controller.HelloFormController"/>
14<bean class="com.durgasoft.controller.WishController"/>
15 
16<!--<bean name="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> -->
17<!--
18<bean name="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
19<property name="mappings">
20 <props>
21 <prop key="/hello">helloFormController</prop>
22 <prop key="/wish">wishController</prop>
23 </props>
24</property>
25</bean>
26-->
27<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
28<property name="caseSensitive" value="true"/>
29</bean>
30<bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
31<property name="prefix" value="/WEB-INF/"/>
32<property name="suffix" value=".jsp"/>
33</bean>
34</beans>
35

ControllerClassHandlerMapping — web.xml

Example24
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
4<display-name>app17</display-name>
5<welcome-file-list>
6<welcome-file>index.html</welcome-file>
7<welcome-file>index.htm</welcome-file>
8<welcome-file>index.jsp</welcome-file>
9<welcome-file>default.html</welcome-file>
10<welcome-file>default.htm</welcome-file>
11<welcome-file>default.jsp</welcome-file>
12</welcome-file-list>
13<servlet>
14<servlet-name>ds</servlet-name>
15<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16<load-on-startup>1</load-on-startup>
17</servlet>
18<servlet-mapping>
19<servlet-name>ds</servlet-name>
20<url-pattern>/</url-pattern>
21</servlet-mapping>
22</web-app>
23

DefaultAnnotationHandlerMapping — index.jsp

org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping is a default HandlerMapping class in Spring WEB MVC applications if we use Annotations , it is not required to configure in Spring configuration File.

It will map incoming request URI with the name provided along with the @RequestMapping annotation which we provided above of the controller class or Controller class method.

EX:

Example25
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<jsp:forward page="wish"/>
12</body>
13</html>
14

DefaultAnnotationHandlerMapping — helloform.jsp

Example26
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: red;" align="center"> Durga Software Solutions </h2>
12<h3 style="color: blue;" align="center"> User Hello Form </h3>
13<form method="POST" action="wish">
14<center>
15<table>
16<tr>
17<td>User Name</td>
18<td><input type="text" name="uname"/></td>
19</tr>
20<tr>
21<td><input type="submit" value="SayHello"/></td>
22</tr>
23</table>
24</center>
25</form>
26</body>
27</html>
28

DefaultAnnotationHandlerMapping — wish.jsp

Example27
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: red;" align="center"> Durga Software Solutions </h2>
12<h3 style="color: blue;" align="center"> User Hello Status </h3>
13<h1 style="color: red;" align="center"> Hello ${uname} </h1>
14</body>
15</html>
16

DefaultAnnotationHandlerMapping — WishController.java

Example28
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7import org.springframework.stereotype.Controller;
8import org.springframework.web.bind.annotation.RequestMapping;
9import org.springframework.web.bind.annotation.RequestMethod;
10import org.springframework.web.servlet.ModelAndView;
11@Controller
12@RequestMapping("/wish")
13public class WishController{
14@RequestMapping(method=RequestMethod.GET)
15public String helloForm() {
16return "helloform";
17}
18@RequestMapping(method=RequestMethod.POST)
19public ModelAndView wish(HttpServletRequest request, HttpServletResponse response) throws Exception {
20String uname = request.getParameter("uname");
21return new ModelAndView("wish", "uname", uname);
22}
23}
24

DefaultAnnotationHandlerMapping — ds-servlet.xml

Example29
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:p="http://www.springframework.org/schema/p"
6xmlns:context="http://www.springframework.org/schema/context"
7xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context.xsd">
12 
13<bean class="com.durgasoft.controller.WishController"/>
14 
15<!--<bean name="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> -->
16<!--
17<bean name="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
18<property name="mappings">
19 <props>
20 <prop key="/hello">helloFormController</prop>
21 <prop key="/wish">wishController</prop>
22 </props>
23</property>
24</bean>
25 
26<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
27<property name="caseSensitive" value="true"/>
28</bean>
29-->
30<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
31 
32<bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
33<property name="prefix" value="/WEB-INF/"/>
34<property name="suffix" value=".jsp"/>
35</bean>
36</beans>
37

DefaultAnnotationHandlerMapping — web.xml

Example30
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
4<display-name>app17</display-name>
5<welcome-file-list>
6<welcome-file>index.html</welcome-file>
7<welcome-file>index.htm</welcome-file>
8<welcome-file>index.jsp</welcome-file>
9<welcome-file>default.html</welcome-file>
10<welcome-file>default.htm</welcome-file>
11<welcome-file>default.jsp</welcome-file>
12</welcome-file-list>
13<servlet>
14<servlet-name>ds</servlet-name>
15<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16<load-on-startup>1</load-on-startup>
17</servlet>
18<servlet-mapping>
19<servlet-name>ds</servlet-name>
20<url-pattern>/</url-pattern>
21</servlet-mapping>
22</web-app>
23

DefaultAnnotationHandlerMapping

Note: In Single Spring WEB MVC applications we are able to configure more than one HandlerMapping classes in spring configuration file, if we want to provide a particular order to execute them then we have to declare "order" property with a particular value at each and every HandlerMapping class, where if the order value is low then the HandlerMapping class will get high priority and if the order value is high then the respective Handlermapping class will get less priority.

Example31
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans ...>
4<bean class="com.durgasoft.controller.WishController"/>
5<bean name="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
6 <property name="order" value="1"/>
7</bean>
8<bean name="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
9<property name="mappings">
10 <props>
11 <prop key="/hello">helloFormController</prop>
12 <prop key="/wish">wishController</prop>
13</props>
14</property>
15<property name="order" value="2"/>
16</bean>
17 
18<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
19<property name="caseSensitive" value="true"/>
20<property name="order" value="3"/>
21</bean>
22<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
23 <property name="order" value="4"/>
24</bean>
25<bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
26<property name="prefix" value="/WEB-INF/"/>
27<property name="suffix" value=".jsp"/>
28</bean>
29</beans>
30
📝 Key Takeaways
  • The mapping is what turns a URL into a controller; DispatcherServlet does not guess
  • BeanNameUrl is the simplest, SimpleUrl the most explicit, ControllerClassName the most implicit
  • Annotation-driven mapping keeps the URL next to the method that serves it