Eclipse & Tomcat Error: HTTP Status 404 – Not Found:

Eclipse & Tomcat Error: HTTP Status 404 – Not Found:

描述: 源服务器未找到目标资源的当前表示或不愿公开表示存在。

这是我的家庭控制器Class:

package com.luv2code.springdemo.mvc;

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

@Controller
public class HomeController {

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

这是我的Web.xml文件:

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

    <!-- 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>

这是spring-mvc-demo-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"
    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.luv2code.springdemo" />

    <!-- 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>

这是主要的-menu.jsp文件

<!DOCTYPE>
<html>

<body>

<h2>Spring MVC Demo - Home Page</h2>

<hr>

<a href="showForm">Hello World form</a>
</body>

</html>

这是目录的图片

Directory Image in Eclipse

PS: 我已经尝试了所有其他选项,包括切换位置、清理目录并重新安装 tomcat 和 eclipse,但所有这些都不起作用对于 me.Even,我将 IDE 从 Eclipse 更改为 IntelliJ Idea,但问题仍然存在。

由于您的 servlet 名称是 'dispatcher' Spring 在您的 WEB-INF 文件夹的默认位置查找文件 dispatcher-servlet.xml。 尝试将 spring-mvc-demo-servlet.xml 重命名为 dispatcher-servlet.xml 它将 work.Hope 有所帮助,谢谢

确保您的包名称是一致的。确认您为控制器创建的包与 spring-servlet xml 文件中定义的相同(为组件扫描定义)。 和 清除 Tomcat 缓存 这通常是缓存的缓存问题。

以下是清除 Eclipse 缓存和 Tomcat 缓存的一些步骤。

  1. 在 Eclipse 的服务器选项卡中,停止 Tomcat 服务器

  2. 右键单击服务器并select“清理...”

  3. 再次右键单击服务器并select“清理Tomcat工作目录...”

  4. 在 Eclipse 中,select 顶层菜单选项,Project > Clean ...

  5. 确保您的项目已 selected 并单击“确定”

  6. 重新启动 Eclipse