Spring MVC 获取过去3周的HTTP状态404错误

0pizxfdo  于 2022-11-15  发布在  Spring
关注(0)|答案(1)|浏览(162)

我试图解决这个错误的最后3个星期。我尝试了一些解决方案,从StackOverflow,但他们没有帮助我也尝试看youtube视频,但结果仍然是一样的。

这是我的POM.xml

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.springMVC</groupId>
  <artifactId>com.springMVC</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>com.springMVC Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.21.RELEASE</version>
    </dependency>


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>com.springMVC</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

这是我的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>

  <!--Configuring DispatcherServlet-->
  <servlet>
    <servlet-name>ser</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>ser</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

这是我的Servlet程式码

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

    <context:component-scan base-package="Controller" />
<!--    <context:annotation-config />-->

    <!--    Configuring View Resolver-->
    <bean name="viewRes" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>

这是我的控制器代码

package Controller;

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

@Controller
public class AllControllers {
    @RequestMapping("/home")
    public String index(){
        return "index";
    }

    @RequestMapping("/about")
    public String about(){
        return "about";
    }
}

这是我的index.jsp和about.jsp代码

  • 关于.jsp*
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>about</title>
</head>
<body>
<h1>ABOUT</h1>
</body>
</html>
  • 索引.jsp*
<html>
<body>
<h2>Hello World!</h2>
<h2>Hello Sanan!</h2>
<h2>Hello Shaikh!</h2>
</body>
</html>

文件夹结构

我被过去三周的错误卡住了

6gpjuf90

6gpjuf901#

您一直在使用Tomcat 10.x,而此版本的tomcat使用的是Servlet API版本5.0。Servlet API 5是Jakarta EE版本9的一部分。自Jakarta EE版本9以来,javax.*包已重命名为jakarta.*包。因此,您在启动tomcat服务器时可能会遇到以下问题。
java.lang.ClassNotFoundException:javax.servlet.http.HttpServlet
您可以将Tomcat版本降级到9.x,也可以修改tomcat server 10的context.xml文件,如下所述。

<Context>
     ...
     <Loader jakartaConverter="TOMCAT" />
</Context>

重新启动服务器,您可以通过点击以下端点来访问资源。
http://localhost:8080/com.springMVC/home
http://localhost:8080/com.springMVC/about

相关问题