You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC示例程序404 Not Found错误排查求助

Troubleshooting Spring MVC 404 Not Found Error

Hey there, let's work through this 404 issue step by step—you've already tried some solid fixes (adding Maven dependencies to Deployment Assembly and adjusting Tomcat paths), so let's dig into other potential culprits based on your code and setup.

First, let's recap your setup to make sure we're on the same page:

  • You're using Eclipse JEE 2019-09, JRE 1.8, Spring 4.1
  • Your Controller maps to /hello and returns "Hello"
  • DispatcherServlet is configured with component scanning for com.practice.controllerclasses
  • View resolver points to /WEB-INF/[viewName].jsp
  • web.xml maps the DispatcherServlet to /

Let's check these key areas:

1. Verify DispatcherServlet Configuration File Name & Location

Your web.xml defines the servlet name as Spring—by default, Spring looks for a configuration file named Spring-servlet.xml in the WEB-INF directory. If your configuration file has a different name (e.g., dispatcher-servlet.xml), Spring won't load it, meaning your Controller won't be detected.

Double-check:

  • Is your XML configuration file named exactly Spring-servlet.xml?
  • Is it placed directly in the WEB-INF folder (not a subfolder)?

2. Confirm Controller Package Matching

Your component scan is set to com.practice.controllerclasses:

<context:component-scan base-package="com.practice.controllerclasses" />

Make sure your ControllerClass is actually located in this package. Even a tiny typo (e.g., controllerclass instead of controllerclasses) will prevent Spring from picking up the Controller.

3. Check JSP File Existence & Path

Your view resolver is configured to look for JSPs at /WEB-INF/[returnValue].jsp. When your Controller returns "Hello", Spring expects a file named Hello.jsp in the WEB-INF directory.

  • Does Hello.jsp exist in /WEB-INF/?
  • Watch out for case sensitivity: On some servers (like Tomcat running on Linux), hello.jsp won't match Hello.jsp—make sure the case matches exactly what you return from the Controller.

4. Validate Request URL

When accessing the endpoint, make sure you're using the full correct URL:

http://localhost:8080/[your-project-name]/hello

It's easy to forget the project name (the context path) after the port number. If you're not sure what your project's context path is, check Eclipse's Servers tab: right-click your Tomcat server → Properties → Web Module → Context Root.

5. Check Tomcat Deployment & Startup Logs

  • In Eclipse, go to the Servers tab, right-click your Tomcat instance → Add and Remove—ensure your project is listed under "Configured" (not "Available").
  • Start Tomcat and check the Console tab for Spring startup logs. You should see lines like:
    INFO: Initializing Spring FrameworkServlet 'Spring'
    INFO: FrameworkServlet 'Spring': initialization started
    INFO: FrameworkServlet 'Spring': initialization completed in X ms
    
    If you don't see these logs, the DispatcherServlet isn't being loaded correctly—double-check your web.xml syntax (it looks correct, but sometimes subtle XML errors can cause this).

6. Confirm Maven Dependencies

Ensure your pom.xml includes all required Spring MVC dependencies for 4.1. At minimum, you should have:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>

Missing dependencies can cause silent failures where components aren't loaded properly.

Your Provided Code for Reference

Controller Class

@Controller
public class ControllerClass{
    @RequestMapping("hello")
    public String redirect() {
        return "Hello";
    }
}

DispatcherServlet Configuration (Spring-servlet.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       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/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 ">
    <context:component-scan base-package="com.practice.controllerclasses" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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_3_1.xsd">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Try working through these checks one by one—most 404 issues in Spring MVC boil down to one of these configuration mismatches. Let me know if any of these fix the problem!

内容的提问来源于stack exchange,提问作者kartik hegde

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:32:33