Wednesday 23 January 2019

ITV-Spring MVC- Spring MVC Handler Mapping Example-xml based configuration

Spring MVC Handler Mapping Example


 1.BeanNameUrlHandlerMapping

The BeanNameUrlHandlerMapping class maps URL requests to beans names. It is the default handler mapping class, so it is the one created by the DispatcherServlet when Spring cannot find any handler mapping class declared. An example of using the BeanNameUrlHandlerMapping class is shown below. There are two beans declared, the first one’s name is helloWorld.htm and its class is the HelloWorldController. So the BeanNameUrlHandlerMapping will map any helloWorld URL request to this Controller. The second bean’s name is the hello*.htm and its class is also the HelloWorldController. So, in this case, the BeanNameUrlHandlerMapping will map any URL request that starts with hello (such as helloWorldhelloAll) to the HelloWorldController.
mvc-dispatcher-servlet.xml
04    xsi:schemaLocation=" http://www.springframework.org/schema/beans    
07
08    <bean
09        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
10        <property name="prefix">
11            <value>/WEB-INF/</value>
12        </property>
13        <property name="suffix">
14            <value>.jsp</value>
15        </property>
16    </bean>
17  
18   <bean
19    class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
20  
21   <bean name="/helloWorld.htm"
22        class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
23  
24   <bean name="/hello*.htm"
25        class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
26  
27</beans>
So, check what happens when the calling the URL helloWorld.htm:
BeanNameUrlHandlerMapping1
And here is the case of helloGeeks.htm:
BeanNameUrlHandlerMapping2

5. ControllerClassNameHandlerMapping

The ControllerClassNameHandlerMapping class uses a convention to determine the mapping between request URLs and the Controller instances that are to handle those requests. In this case, there is no need to declare a bean name for the Controller. In the example below, the ControllerClassNameHandlerMapping will map to the HelloWorldController all URL requests that start with helloWorld, or helloWorld*. In the ControllerClassNameHandlerMapping bean declaration there are two properties to configure, the caseSensitive, which is set to true, and the pathPrefix, which is set to /javacodegeeks/. These properties allow ControllerClassNameHandlerMapping to also map to the HelloWorldController all URL requests with uppercase characters, like helloWorldJavaCodeGeeks, as also URL requests with path prefix like /javacodegeeks/helloWorld.
mvc-dispatcher-servlet.xml
01....
02<bean
03   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
04   <property name="caseSensitive" value="true" />
05     <property name="pathPrefix" value="/javacodegeeks" />
06   </bean>
07
08  
09   <bean class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
10...
The cases described above are shown in the screenshots below.
Here is a case of uppercase characters:
ControllerClassNameHandlerMapping2
And here is a case with a pathPrefix:
ControllerClassNameHandlerMapping1

6. SimpleUrlHandlerMapping

The SimpleUrlHandlerMapping provides a property called mappings so as to be configured. This property is set in the bean declaration and consists of key value mapping pairs. It can be set in two ways, as shown below:
mvc-dispatcher-servlet.xml
01....
02<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
03       <property name="mappings">
04        <props>
05           <prop key="/helloWorld.htm">helloWorldController</prop>
06           <prop key="/*/hello.htm">helloWorldController</prop>
07           <prop key="/hello*.htm">helloWorldController</prop>
08         </props>
09       </property>
10    </bean>
11
12   <bean id="helloWorldController"class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
13...
mvc-dispatcher-servlet.xml
01....
02<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
03       <property name="mappings">
04         <value>
05           /helloWorld.htm=helloWorldController
06           /*/hello.htm=helloWorldController
07           /hello*.htm=helloWorldController
08        </value>
09       </property>
10    </bean>
11
12   <bean id="helloWorldController"class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
13....
Note that the Controller bean declaration uses an id property, which is used in the SimpleUrlHandlerMapping bean declaration for the mapping. Each one of the cases configured above, are shown in the screenshots below:
SimpleUrlHandlerMapping1
SimpleUrlHandlerMapping2
SimpleUrlHandlerMapping3

7. Handler mapping priorities

The handler mapping implementations described can be mixed and used together. The only thing that needs to be configured is the priority of each mapping class, so that Spring MVC DispatcherServlet will know which handler mapping implementation to use with what priority. The priority can be set as a property in every mapping bean declaration, as shown below:
mvc-dispatcher-servlet.xml
01...
02<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
03       <property name="mappings">
04         <value>
05           /helloWorld.htm=helloWorldController
06           /*/hello.htm=helloWorldController
07           /hello*.htm=helloWorldController
08        </value>
09       </property>
10<property name="order" value="0" />
11    </bean>
12
13   <bean id="helloWorldController"class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
14
15
16<bean
17   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
18   <property name="caseSensitive" value="true" />
19     <property name="pathPrefix" value="/javacodegeeks" />
20    <property name="order" value="1" />
21   </bean>
22
23  
24   <bean class="com.javacodegeeks.snippets.enterprise.HelloWorldController" />
25...
In this case, both ControllerClassNameHandlerMapping and SimpleUrlHandlerMapping are used, but the first one to handle a URL request will be the SimpleUrlHandlerMapping.

This was an example of how to handle requests mapping in Spring MVC.
Download source code from this tutorial : SpringMVCHandlerMappingExample

No comments:

Post a Comment

40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot

  40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot 1. What is Tight Coupling? When a class (ClassA) is depen...