当前位置: 首页 > 知识库问答 >
问题:

HTTP状态404–在简单的Spring MVC演示应用程序期间未找到错误

慕学海
2023-03-14

我是Spring的新手,正在尝试MVC。我构建了简单的演示应用程序并在 tomcat 9.0.41 上运行它似乎工件已正确删除,问题似乎出在控制器映射中......它根本找不到我的 JSP。

这是我的项目结构

这是web.xml

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

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

<!--    <context-param>-->
<!--        <param-name>contextConfigLocation</param-name>-->
<!--        <param-value>/WEB-INF/applicationContext.xml</param-value>-->
<!--    </context-param>-->
<!--    <listener>-->
<!--        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
<!--    </listener>-->
    <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>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring-mvc-demo-servlet.xml

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

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

<!--    <context-param>-->
<!--        <param-name>contextConfigLocation</param-name>-->
<!--        <param-value>/WEB-INF/applicationContext.xml</param-value>-->
<!--    </context-param>-->
<!--    <listener>-->
<!--        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
<!--    </listener>-->
    <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>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

家庭控制器

package com.springmvcdemo;

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

@Controller
public class HomeController {

    @RequestMapping("/")
    public String showPage(){
       return "main-menu";
    }
}

和简单的main-menu.jsp

<html>
<body>
<h2>
    Spring Demo MVC - Home page
</h2>
</body>

</html>

这是服务器日志

"C:\Program Files\Apache Software Foundation\Tomcat 9.0\bin\catalina.bat" run
[2021-02-03 07:52:45,945] Artifact SpringMVCdemo:war exploded: Waiting for server connection to start artifact deployment...
Using CATALINA_BASE:   "C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Tomcat_9_0_41_SpringMVCdemo"
Using CATALINA_HOME:   "C:\Program Files\Apache Software Foundation\Tomcat 9.0"
Using CATALINA_TMPDIR: "C:\Program Files\Apache Software Foundation\Tomcat 9.0\temp"
Using JRE_HOME:        "C:\Program Files\Java\jdk-15.0.1"
Using CLASSPATH:       "C:\Program Files\Apache Software Foundation\Tomcat 9.0\bin\bootstrap.jar;C:\Program Files\Apache Software Foundation\Tomcat 9.0\bin\tomcat-juli.jar"
Using CATALINA_OPTS:   ""
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
03-Feb-2021 19:52:46.548 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/9.0.41
03-Feb-2021 19:52:46.549 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Dec 3 2020 11:43:00 UTC
03-Feb-2021 19:52:46.549 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 9.0.41.0
03-Feb-2021 19:52:46.549 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 10
03-Feb-2021 19:52:46.549 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Версия ОС:             10.0
03-Feb-2021 19:52:46.549 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Архитектура:           amd64
03-Feb-2021 19:52:46.550 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             C:\Program Files\Java\jdk-15.0.1
03-Feb-2021 19:52:46.550 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Версия JVM:            15.0.1+9-18
03-Feb-2021 19:52:46.550 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
03-Feb-2021 19:52:46.550 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Tomcat_9_0_41_SpringMVCdemo
03-Feb-2021 19:52:46.550 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 9.0
03-Feb-2021 19:52:46.551 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
03-Feb-2021 19:52:46.551 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
03-Feb-2021 19:52:46.551 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
03-Feb-2021 19:52:46.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Tomcat_9_0_41_SpringMVCdemo\conf\logging.properties
03-Feb-2021 19:52:46.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
03-Feb-2021 19:52:46.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
03-Feb-2021 19:52:46.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
03-Feb-2021 19:52:46.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
03-Feb-2021 19:52:46.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.password.file=C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Tomcat_9_0_41_SpringMVCdemo\jmxremote.password
03-Feb-2021 19:52:46.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.access.file=C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Tomcat_9_0_41_SpringMVCdemo\jmxremote.access
03-Feb-2021 19:52:46.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
03-Feb-2021 19:52:46.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
03-Feb-2021 19:52:46.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
03-Feb-2021 19:52:46.553 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
03-Feb-2021 19:52:46.553 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\User\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Tomcat_9_0_41_SpringMVCdemo
03-Feb-2021 19:52:46.553 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 9.0
03-Feb-2021 19:52:46.553 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=C:\Program Files\Apache Software Foundation\Tomcat 9.0\temp
03-Feb-2021 19:52:46.563 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.25] using APR version [1.7.0].
03-Feb-2021 19:52:46.563 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
03-Feb-2021 19:52:46.563 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
03-Feb-2021 19:52:46.566 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1g  21 Apr 2020]
03-Feb-2021 19:52:46.723 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
03-Feb-2021 19:52:46.748 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [347] milliseconds
03-Feb-2021 19:52:46.785 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
03-Feb-2021 19:52:46.785 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.41]
03-Feb-2021 19:52:46.791 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
03-Feb-2021 19:52:46.797 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [49] milliseconds
Connected to server
[2021-02-03 07:52:47,043] Artifact SpringMVCdemo:war exploded: Artifact is being deployed, please wait...
[2021-02-03 07:52:47,364] Artifact SpringMVCdemo:war exploded: Artifact is deployed successfully
[2021-02-03 07:52:47,364] Artifact SpringMVCdemo:war exploded: Deploy took 321 milliseconds
03-Feb-2021 19:52:56.801 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Установка веб приложения в папку [C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\manager]
03-Feb-2021 19:52:56.879 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\manager] has finished in [78] ms

我已经尝试了@RequestMap(value="/", method= RequestMethod.GET)和("/main-menu")。为类似的错误提供堆栈并使用建议,但没有任何效果。尝试从头开始创建不同的项目……这快把我逼疯了,似乎一切都按照书本做了,但仍然错误。

共有3个答案

黄昊
2023-03-14

我发布了错误的 spring-mvc-demo-servlet.xml代码

<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.springmvcdemo" />

    <!-- 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/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>```
江光明
2023-03-14

这是完整的文档,以防你还没有看过:配置视图控制器

壤驷向明
2023-03-14

为什么你的spring-mvc-demo-servlet.xml文件是web.xml的精确副本?尝试将spring-mvc-demo-servlet.xml更改为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
 
    <mvc:annotation-driven />
    <context:component-scan base-package="com.springmvcdemo" />
    <mvc:default-servlet-handler />
    <mvc:view-controller path="/" view-name="main-menu"/>
 
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
 
</beans>

或。。。

在< code>web.xml中,按如下方式更改映射:

<servlet-mapping>
   <servlet-name>dispatcher</servlet-name>
   <url-pattern>/demo/*</url-pattern>
</servlet-mapping>

您将在您的 url 中附加演示作为 http://localhost:8080/demo/

 类似资料:
  • 这是我在SpringMVC中使用Maven的第一个应用程序。下面是应用程序结构。 控制器代码: 当我点击index.jsp中的链接时,页面应该被遍历到helloworld.jsp

  • 这是一个启动应用程序。它运行完美,但没有得到输出(它显示我的HTTP状态404错误在浏览器) 波姆。xml 启动类Main方法 控制器在主类后加载 网址:http://localhost:8080/hello输出

  • 我已经为servlet编写了这段代码 我已经编译了它,它对应的html文件action属性值是 web.xml包含 servlet name four servlet类Httpservlet1 servlet-name 4 url-模式 /HTTPSERVLET在xml代码格式下运行时仍然显示错误信息

  • 我想创建一个Spring MVC项目。当我在tomcat localhost上使用运行配置(http://localhost:8080/springmvc/hello或http://localhost:8080/springmvc或http://localhost:8080/hello)运行项目时,我得到了以下错误: HTTP状态404-未找到 类型状态报告 消息请求的资源[/SpringMVC]

  • 我正面临着Tomcat404错误的确切问题,并尝试了所有的建议,但没有什么能帮助我修复这个问题。Apache Tomcat9.0和Eclipse Oxywood是我目前使用的,并遵循YouTube上的教程。

  • 我正在按照一些教程从头开始建立一个spring mvc项目。最初,我将web应用程序配置为在Tomcat中运行。我编写了一个简单的controller类,请求点击“/”。但tomcat总是向我显示“HTTP状态404-找不到”错误。 我看了很多这样的问题,但那些答案对我都不起作用 删除tomcat安装并重新配置。当点击localhost:8080/时,我将获得tomcat主页。 检查了代码的拼写

  • 我试图用rest API编写一个与postgres数据库连接的基本应用程序,以执行两个crud操作。当我运行应用程序时,它会成功启动。但是,当我转到url“http://localhost:8080/employees”或http://localhost:8080“时,它显示”无法找到此localhost:8080“。找不到网址为http://localhost:8080/employees的网页

  • 我正在尝试使用Payara服务器为Eclipse中的预订系统开发Jersey服务器应用程序。当我运行这个项目时,我会得到“HTTP状态404-未找到”。我查看了一些教程和StackOverflow帖子,但没有找到错误。有人能帮我吗? BookingService界面: BookingService实现: 波姆。xml: 编辑:当我研究这个问题时,我在网上读到。使用jersey 2时,xml文件不必