Wednesday, 1 February 2012

Handler Mapping in Spring Framework






HandlerMapping is used to determine which controller the request should be sent to.


There are four types of handlerMapping in the spring framework.


1. BeanNameUrlHandlerMapping
2. SimpleUrlHandlerMapping
3. CotrollerClassNameHandlerMaping
4. CommonPathMapHandlerMapping


By default dispatcherServlet look for BeanNameUrlHandlerMapping.




you can use multiple handlerMapping in your application.


for that you have to give order to HandlerMapping.
for eg.



<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/index.htm=welcomeController
/welcome.htm=welcomeController
</value>
</property>
<property name="order" value="0" />
</bean>

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

<bean id="welcomeController" 
class="com.krunal.controller.WeController" />

<bean class="com.krunal.controller.HelloController" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>


</beans>

In this spring-context.xml  two handlerMapping is used. 
And each handler has its property named order and its value.

Lower value means higher priority.



No comments:

Post a Comment