Home > database >  Spring MVC can't find views, but instead return 404 error
Spring MVC can't find views, but instead return 404 error

Time:10-13

i look for the solution about my problem but none of them seem to work on me, so i'm gonna ask, i'm practicing Spring MVC, i can return the jsp file from "webapp" folder, but the moment i move it to a folder, then move the folder to WEB-INF folder, it just stop working:

Here is my layout: enter image description here

This is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <servlet>
        <servlet-name>SpringConfig</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringConfig</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Here is my SpringConfig-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"
    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-4.3.xsd        
        ">
    <context:component-scan base-package="com.SpringWebMvc"/>


    <bean >
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

Here is my HomeController:

package com.SpringWebMvc.Controller;

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

@Controller
public class HomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "user/index";
    }

}

Here is my index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home page</title>
</head>
<body>
    <p>hello</p>
</body>
</html>

Here is my 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.locnt.spring-web-mvc</groupId>
  <artifactId>SpringWebMvc</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  
  <build>
        <plugins>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version> <!-- or whatever current version -->
                <configuration>
                      <source>17</source>
                      <target>17</target>
                </configuration>            
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
            </plugin>
        </plugins>
  </build>
    
  <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.22</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        
  </dependencies>   

  
</project>

And this is what i get: enter image description here

I am completely clueless about what the hell is happening, please help me :(

CodePudding user response:

Seems like the issue is related to fact that in SpringConfig-servlet.xml you have:

<property name="prefix" value="/WEB-INF/views/"/>

but views are located in webapp/views.
You can try moving them to /WEB-INF/views/ folder.

CodePudding user response:

After 2 days of thinking finding what the hell happen to my code, i've finnaly solve it, the thing is it's not my code, but my DAMN SERVER, i am using tomcat v10 at the moment, so i just downgrade to v9 instead, and woala!! it work as it suppose to be

  • Related