Skip to content
Related Articles
Open in App
Not now

Related Articles

Spring MVC – Multiple Resolver Mapping

Improve Article
Save Article
  • Last Updated : 24 Mar, 2022
Improve Article
Save Article

Spring MVC Framework provides the feature of View Resolver through which you can map a view to its respective view and render the model in your browser. The View Resolver eliminates the use of view technologies like Velocity and JSP. There are many view resolvers available in spring-like InternalResourceViewResolver, XmlViewResolver, ResourceBundleViewResolver, etc all having a different way to handle view. Spring MVC also supports multiple view resolvers to rely on others if one fails, in case of multiple view resolvers we need to mention the order or the priority of those view resolver that we want them to work according to.

Implementation: Here we ‘will be creating a simple Spring MVC project and implementing multiple view resolvers for a single view page.

Note: Spring MVC is required as a pre-requisite

Steps to Create Project

Step 1: Create a maven project using create a new project, now add maven-archetype-webapp to your project as we are creating a web application. 

Step 2: Add the group Id, Artifact Id and click on finish.

The project would get created with a pom.xml configuration file, the structure of your project would look something like this:

The pom.xml configuration file defines the dependencies that maven gets and manages for your project. After creating the project our pom.xml looks like this below as follows:

File: pom.xml

XML




  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mkyong.common</groupId>
  <artifactId>SpringMVC</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SpringMVC Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
      
    <!-- Spring framework --> 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
    </dependency>
   
    <!-- Spring MVC framework --> 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>2.5.6</version>
    </dependency>
      
    <!-- JSTL --> 
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>
      
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
   
  </dependencies>
  <build>
    <finalName>SpringMVC</finalName>
  </build>
</project>


The web.xml file defines mapping with different URLs and servlets to handle requests for those URLs. In this configuration file, we have used listener for application startup, configured servlet, and added a servlet-mapping to map the URL.

File: web.xml

XML




<web-app id="WebApp_ID" version="2.4" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  
  <display-name>Spring MVC</display-name>
    
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
    
  <servlet>
      <servlet-name>gfg</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
    
  <servlet-mapping>
     <servlet-name>gfg</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/gfg-servlet.xml</param-value>
  </context-param>
    
</web-app>


The gfg-servlet.xml located in  “/src/main/webapp/WEB-INF/gfg.servlet.xml” is the files where we define beans and mapping for our project. This is the file where we have defined the view Resolvers. The InternalResolverViewResolver is a subclass of UrlBasedViewResolver that supports InternalResourceView. The XmlViewResolver supports files written in xml.

File: gfg-servlet.xml

XML




  
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
    <bean class="com.gfg.controller.WelcomeController" />
  
  <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>
          <property name="order" value="1" />
    </bean>
  
    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
       <property name="location">
           <value>/WEB-INF/spring-views.xml</value>
       </property>
       <property name="order" value="0" />
    </bean>
    
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/welcomePage.htm">welomecontroller</prop>
            </props>
        </property>
    </bean>
      
    <bean id="welcomeController" 
        class="org.springframework.web.servlet.mvc.ParameterizableViewController">
        <property name="viewName" value="welcomepage" />
    </bean>
  
      
</beans>


The spring-views.xml page is the xml based view page for our XmlViewResolver to handle, this page defines the bean for welcomepage.

File: spring-views.xml

XML




  
    <bean id="welcomeRedirect" 
       class="org.springframework.web.servlet.view.RedirectView">
       <property name="url" value="welcomepage.htm" />
    </bean>
      
</beans>


The WelcomeController class in the com.gfg.controller handles the incoming URL request and maps it to the view page, here we use ModelAndView and redirect to the welcome page.

FIle: WelcomeController.java

Java




// Java Program to Illustrate WelcomeController Class
  
package com.gfg.controller;
  
// Importing required classes
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
  
// Class
// Extending AbstractController interface
public class WelcomeController extends AbstractController {
  
    @Override
    protected ModelAndView
    handleRequestInternal(HttpServletRequest request,
                          HttpServletResponse response)
        throws Exception
    {
  
        return new ModelAndView("redirect:welcomepage.htm");
    }
}


Step 3: After completing the application, your project structure would look something like this:

Now it’s time to run your project in the Tomcat Server, after running the tomcat server type the URL http://localhost:8080/SpringMvcMultipleViewResolver/welcomepage.htm in your favorite browser.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!