Handler mappings in Spring MVC by R4R Team

Handler mappings in Spring MVC:-

When a request comes to the DispatcherServlet, you may want to take it through different stages. In this framework, you can configure handlers to do this. There are many handlers available that can be used by providing handler mappings. Since you want to take the request through different process steps, the handler must be able to process the request for a definite task and forward the request to next handler in chain.

In previous versions of Spring, users were required to define one or more HandlerMapping beans in the web application context to map incoming web requests to appropriate handlers. With the introduction of annotated controllers, you generally don’t need to do that because the RequestMappingHandlerMapping automatically looks for @RequestMapping annotations on all @Controller beans.
For this it must also contain the list of handlers applied to this request.

HandlerIntercepter: Implementer of this intercept can implement three methods of this intercepter, one that is called before the actual handler execution, second after the handler execution and last one after the complete request processing. Handlers can be configured using SimpleUrlHandlerMapping configuration in application context.

<beans>

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">

<property name="interceptors">

<bean class="example.MyInterceptor"/>

</property>

</bean>

<beans>

BeanNameUrlHandlerMapping: Extends from AbstractHandlerMapping. Maps incoming HTTP request to the bean names configured in application context i.e. as a bean name, in application context, we give url and the bean is of a controller. Thus the url is mapped to a controller.

<beans ...>

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<bean name="/welcome.htm" class="r4r.in.controller.WelcomeController" />

</beans>


SimpleUrlHandlerMapping: Extends from AbstractHandlerMapping. More powerful and supports Ant-style url mapping. This mapping we configure at two places. In web.xml we define the url pattern supported.

<servlet-mapping>

<servlet-name>springMVC</servlet-name>

<url-pattern>*.html</url-pattern>

</servlet-mapping>

In application context xml, we map the urls to controller definitions.

<bean class=”org.springframework.web.servlet.handler.SimpleUrlHandlerMapping”>

<property name=”mappings”>

<value>

/*/HelloWorld.htm= saveHelloWorldController

</value>

</property>

</bean>

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!