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


  <name>SpringMVC Maven Webapp</name>
    <!-- Spring framework --> 
    <!-- Spring MVC framework --> 
    <!-- JSTL --> 

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


<web-app id="WebApp_ID" version="2.4" 
  <display-name>Spring MVC</display-name>

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


    <bean class="" />
    <bean class="com.gfg.controller.WelcomeController" />
  <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
          <property name="suffix">
          <property name="order" value="1" />
    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
       <property name="location">
       <property name="order" value="0" />
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
                <prop key="/welcomePage.htm">welomecontroller</prop>
    <bean id="welcomeController" 
        <property name="viewName" value="welcomepage" />

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


    <bean id="welcomeRedirect" 
       <property name="url" value="welcomepage.htm" />

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.



// 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 {
    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!