Initial Setup

Before starting with the InternalResourceViewResolver configuration we need to have a working Spring MVC project to carry out the configuration. Let’s set it up first.

Step 1: Open your Eclipse IDE or any of your favorite IDEs that support Spring development.
Step 2: Create a maven webapp project.
Step 3: Add the spring context, spring mvc, jakarta-servlet-api dependency in the pom.xml file.

XML




<!-- File Name: pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>springmvc</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springmvc Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.0.9</version>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>springmvc</finalName>
    </build>
</project>


Step 4: Create a folder with the name Java under the main method and add the com/example/controller package inside the java folder.
Step 5: Create a controller named HelloController.java under the controller package and add the below code.

Java




// File Name: HelloController.java
  
package com.example.controller;
  
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
  
import java.util.Map;
  
// Controller for mvc
@Controller
public class HelloController {
    
    // mapping /hello url with get type of request
    // requestparam to get the name value from the
    // get request url, map used to pass value to result.jsp
    @GetMapping("/hello")
    public String getView(@RequestParam("name") String name,
                          Map<String, Object> map)
    {
        
        map.put("name", name);
        return "/WEB-INF/jsp/hello.jsp";
    }
}


Explanation of the above code:

  • Create a Spring controller by annotating the class with @Controller from the Spring framework.
  • A Get request is created at /hello and mapped to the getView() method using GetMapping annotation.
  • Fetch the name parameter from the request object with the @RequestParam annotation.
  • Add a map to transfer the name attribute to the result view.
  • Return the view hello.jsp along with its path starting from webapp folder.

Step 6: Open your web.xml under the /webapp/WEB-INF folder and configure the DispatcherServlet to handle every incoming request.

XML




<!-- File Name: web.xml -->
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
  
<web-app>
  <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>


Explanation of the above code:

  • In Spring MVC, every request will be served via the DispatcherServlet so configure the DispatcherServlet to intercept every request to the “/” root context path.
  • Create a servlet with the <servlet> tag, map the class DispatcherServlet class to it using <servlet-class> child tag, and also provide a name to it using <servlet-name> tag for servlet-mapping.
  • Map “/” URL-pattern to the DispatcherServlet using its servlet-name, here dispatcher is used as servlet-name.

Step 7: Create a servlet config file for the servlet just created inside the web.xml, note the servlet config filename should follow the pattern servlet_name-servlet.xml, where servlet_name is the created servlet name in web.xml. I used dispatcher as the servlet name so the filename is dispatcher-serlvet.xml under the WEB-INF folder.

XML




<!-- File Name: dispatcher-servlet.xml -->
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
      <!-- to use annotation in our classes -->
      <mvc:annotation-driven />
      <!-- scanning for components in the base package "com.example" -->
    <context:component-scan base-package="com.example"></context:component-scan>
</beans>


  • A dispatcher-servlet.xml is created to configure the servlet and add an annotation-driven tag to use the annotation-based configuration in the servlet.
  • Configure the context’s component-scan base package to scan for controllers and configurations.

Step 8: Create an index.jsp file under webapp folder to be accessible for everyone. Call the MVC controller class function from the form to get the JSP view as a response.

HTML




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isELIgnored="false" %>
<html>
<head>
<title>Spring MVC</title>
</head>
<body>
    <form action="hello">
        <label for="name">Enter your name: </label> <input type="text" id="name"
            name="name">
        <button>Submit</button>
    </form>
</body>
</html>


  • A form with the action /hello attribute to call default get mapping.
  • An input tag with a name as name is included to get the name and transfer it to /hello to get mapping.

Step 9: Create a hello.jsp under /WEB-INF/jsp/ folder to return the hello result view from the controller and render the name inserted in the model.

Note: Only add public files under the webapp folder, if you want to secure files from direct access, put it under /webapp/WEB-INF/ as files under WEB-INF can be served only via controllers.

HTML




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isELIgnored="false" %>
    
<!DOCTYPE html>
<html>
  <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
  </head>
  <body>
      <h1>Welcome ${name} !</h1>
  </body>
</html>


Step 10: Deploy this spring mvc webapp in the Tomcat server, this will give you a simple index page to enter your name and upon submitting will welcome you by your name.

initial output

Spring MVC – InternalResourceViewResolver Configuration

Spring MVC is a powerful tool to create web projects provided by the Spring framework. Spring MVC follows the Model-View-Controller Architecture internally to handle and process the data, where the data will be visualized via the View to the user from the Model layer. The interaction will be monitored and handled by the Controller layer of the Architecture.

In Spring MVC, InternalResourceViewResolver is a class that plays a vital role in mapping the logical view name to the actual physical resource view. Using logical view names helps us to achieve code modularity and a flexible way to manage the views.

Prerequisite

  • Java – Core and Advanced Java
  • Spring & Spring MVC
  • Maven & Eclipse or any other IDE’s for Java

Similar Reads

InternalResourceViewResolver

In Spring MVC, the views are returned as a response to the requests, one can directly return the physical resource name but it will make the code tightly coupled to the physical resource, and changing the view templates will be hard. Another way would be...

Initial Setup

Before starting with the InternalResourceViewResolver configuration we need to have a working Spring MVC project to carry out the configuration. Let’s set it up first....

Configuring InternalResourceViewResolver

...

Configuration Method

...

1. XML Configuration

...

2. Java Configuration

...

Using InternalResourceViewResolver

...

Conclusion

...