Spring 5.3.9 MVC XML Configuration: ViewResolver Not Mapping To Controller

823 Views Asked by At

I am beginning to learn the Spring Framework and am currently learning Spring MVC. I am using Spring version 5.3, and I am having issues with loading the JSP files in the views directory. I understand from the notes that they removed suffix pattern matching, but I have no idea what my XML configuration should be to match this new change.

Can someone please help me get this issue sorted, and show me what the XML configuration should be for Spring 5.3 MVC. It's halted my progress for three whole days!

Note: I am aware that Spring Boot is the norm these days but I want to learn Spring from scratch even if it is more tedious to work with.

Additional Note: I am using entirely XML configuration for this.

I am not using a build tool at the moment as I will learn one later on. I build my projects from the command line:

cd into webapp folder --> jar -cvf mvc-test.war * --> run on Tomcat 9 server

JDK: 11

IDE: VS Code

My Controller Class:

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {
    
    @RequestMapping("/")
    public String showPage() {
        return "test-page";
    }
}

My web.xml File:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <display-name>spring-mvc-demo</display-name>

    <absolute-ordering />

    <!-- Spring MVC Configs -->

    <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

My spring-mvc-demo-servlet.xml File:

<?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">

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="com.controller" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

My project structure is below (Sorry I don't know how to format this):

Root Projet Folder: mvc-test

mvc-test: src and webapp folders

src: main/java/com/controller/HomeController.java

webapp: WEB-INF

WEB-INF: lib, views, spring-mvc-demo-servlet.xml, web.xml

views: test-page.jsp

Project Structure

0

There are 0 best solutions below