Search This Blog

Tuesday, 13 November 2012

Using Multiple URL Handler Mappings

As we saw earlier Spring supports a variety of URL handler mappings. In fact we can use multiple URL Handler mappings in the same application.
Consider the XML configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" 
        xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean name="/**/beanName.do" class="com.mvc.controller.BeanNameController" />

    <bean name="simpleUrlController" class="com.mvc.controller.SimpleUrlController" />
    <bean id="simpleUrlHandlerMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /**/simple.do=simpleUrlController
            </value>
        </property>
    </bean>

    <bean name="welcomePageController" class="com.mvc.controller.WelcomePageController" />
    <!-- maps welcomepage.do to "welcomePageController" -->
    <bean id="controllerClassNameHandlerMapping"
        class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    </bean>

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

</beans>
We have used three different controllers:
public class BeanNameController implements Controller {

    @Override
    public ModelAndView handleRequest(final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {
        ModelAndView modelAndView = null;
        User user = new User();
        user.setAge(18);
        user.setName("Bean Name URL Mapping");
        modelAndView = new ModelAndView("data", "user",user);
        return modelAndView;
    }

}
All URLs ending in "beanName.do" are mapped to BeanNameController.
The second class is SimpleUrlController.
public class SimpleUrlController implements Controller {

    @Override
    public ModelAndView handleRequest(final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {
        ModelAndView modelAndView = null;
        User user = new User();
        user.setAge(18);
        user.setName("Simple URL Mapping");
        modelAndView = new ModelAndView("data", "user",user);
        return modelAndView;
    }

}
All URLs of type "simple.do" are mapped to the above class.
The third controller is WelcomePageController
public class WelcomePageController implements Controller {

    @Override
    public ModelAndView handleRequest(final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {
        ModelAndView modelAndView = null;
        User user = new User();
        user.setAge(27);
        user.setName("ControllerClassName Handler Mapping");
        modelAndView = new ModelAndView("data", "user", user);
        return modelAndView;
    }

}
In case of this controller, the URL pattern is the class name minus the Controller. In this case the URL is welcomepage.do (The class name in lowercase is used.)
In case of all controllers the same jsp file was used.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Spring MVC Minimal</title>
    </head>
    <body>
        This is the model based view as JSP
        <br/>The user is <%=request.getAttribute("user") %>
    
    </body>
</html>
The index page I used to start test the application:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Spring MVC Minimal</title>
    </head>
    <body>
        <br/>For welcome <a href="/SpringMVCMultipleMappings/welcomepage.do">here</a>
        <br/>For simple <a href="/SpringMVCMultipleMappings/simple.do">here</a>
        <br/>For bean name <a href="/SpringMVCMultipleMappings/beanName.do">here</a>
    </body>
</html>
Spring also allows us to prioritize the URL Handler mappings. For this there is an order property available to which numeric values are assigned. Lower the value, higher the priority.
<bean name="/**/beanName.do" class="com.mvc.controller.BeanNameController"/>
<bean id ="beanNameUrlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
    <property name="order" value="3"/>
</bean>
    
<bean name="simpleUrlController" class="com.mvc.controller.SimpleUrlController"/>
<bean id="simpleUrlHandlerMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /**/welcomepage.do=simpleUrlController
        </value>
    </property>
    <property name="order" value="0"/>
</bean>

<bean name="welcomePageController" class="com.mvc.controller.WelcomePageController"/>
<!-- maps welcomepage.do to  "welcomePageController" -->
<bean id="controllerNameHandlerMapping"
    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    <property name="order" value="2"/>
</bean>
The property is inherited from the Ordered interface.For incoming requests, the DispatcherServlet consults the URL handler mappings in the order of priority. On finding a match, it delegates control to the appropriate controller.
The welcomepage.do can be resolved by two controllers - simpleUrlController and welcomePageController. But becauce of its higher priority, the request will be delegated to simpleUrlController

12 comments: