简体   繁体   中英

HTTP Status 404 - not found - Java Spring MVC

I'm trying to run my first Spring MVC Program on eclipse using XML configuration, I have no error markings on my directory but I get this response when I run the program. I also tried with Spring java configuration and got similar response. bug

I use eclipse Version: 2021-06 (4.20.0) and apache-tomcat-10.0.13 installed on my device, sending a request at http://localhost:8080/ returns apache-tomcat successfully installed. So far I have tried all suggestions I found online including updating maven project, maven build-clean install, cleared file cache, deleted temp folder in my root director changed workspace directory, uninstalled and reinstall apache-tomcat multiple times, cleared missing files on source directory in java build path but none of this approaches have worked. Below is a screenshot of my program (XML configuration)

Directory

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>org.idevelope</groupId>
  <artifactId>spring-mvc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Spring MVC</name>
  <description>Spring MVC example</description>
  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.14</version>  
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
 <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>16</release>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
      </plugin>
    </plugins>
  </build>
</project>

web.xml

dispatcher-servlet.xml

controller

user.java

(View Pages)

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC tutorial - Home JSP</title>
</head>
<body>
  <h1>Hello World!</h1>
  <h4>Message- </h4><span>${message}</span>
  <form:form action="showUser" modelAttribute="user" method="post">
    <tr>
      <td>
        <form:label path="firstName">First Name</form:label>
      </td>
      <td>
        <form:input path="firstName" id="firstname" />
      </td>
    </tr>
    <tr>
      <td>
        <form:label path="lastName">Last Name</form:label>
      </td>
      <td>
        <form:input path="lastName" id="lastname" />
      </td>
    </tr>
    <input type="submit" value="Submit">
  </form:form>
</body>
</html>

user.jsp

Please I need help to fix this bug.

In my case and as i searched internet - tomcat 10 does not work. I downloaded tomcat 9 and 404 disappeared. Spring MVC 5 does not work on Tomcat 10. This is because Tomcat 10 is based on Jakarta EE 9 where package names for APIs have changed from javax.* to jakarta.*.

maybe lack *<mvc:annotation-driven/>*Annotation-driven injection (eg Controller, @requestMapping, etc.) results in a page always indicating that the relevant resource information cannot be accessed

You write, you try to run your first spring program with Spring MVC. I did not dive into your details, but I would suggest two different approaches to learn and play around with Spring MVC:

Spring initializer web site

When I try out new spring parts, I always start at https://start.spring.io/ and just add my wanted dependencies (search for mvc when pressing add button).

After defined your setting there, you can just download a working maven or a gradle example directly from the web page as a zip file.After extraction the files can be directly imported by IDE (eg eclipse, intelliJ, etc.)

Official Spring MVC tutorial

When you are searching a good and out-of-the-box working MVC example you should look at https://spring.io/guides/gs/serving-web-content/

This works well and you can start directly without dependency problems etc.

Try adding tomcat jasper dependency to pom.xml

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>10.1.0-M5</version>
</dependency>

check tomcat version before adding the dependency

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM