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

使用Maven搭建SpringMVC时出现Error 404问题求助

Maven SpringMVC 404 Error Troubleshooting & Fixes

Hey there, sorry you're stuck on this frustrating 404 issue—let's break down what's going on here and walk through practical fixes step by step.

Your Current Situation

  • Accessing http://localhost:8080 works fine and shows your "HELLO WORLD" page
  • Directly visiting http://localhost:8080/index.jsp or http://localhost:8080/WebApp/index.jsp throws a 404 error
  • Renaming the controller method from index() to test() didn't resolve the problem

First, let's recap your project configurations (thanks for sharing these details!):


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_3_1.xsd" version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

dispatcher-servlet.xml

<?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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="test.app" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value = "/WEB-INF/views/"/>
<property name="suffix" value=".jsp" />
</bean>
</beans>

applicationContext.xml

无内容

index.jsp

<%-- Created by IntelliJ IDEA. User: reaganyuan Date: 12/31/17 Time: 12:42 PM To change this template use File | Settings | File Templates. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<p>HELLO WORLD</p>
</body>
</html>

TestController.java

package test.app;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping(value="/")
public String test() {
return "index";
}
}

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>test</groupId>
<artifactId>app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
</dependencies>
</project>

What's Causing the 404s?

Let's unpack each issue:

  1. Direct index.jsp access fails
    Your DispatcherServlet is mapped to /, meaning it handles all incoming requests—including static files like .jsp. When you hit http://localhost:8080/index.jsp, the servlet looks for a controller mapping for /index.jsp (which doesn't exist), so it returns a 404. The root path works because your TestController maps to / and returns "index", which your ViewResolver resolves to /WEB-INF/views/index.jsp.

  2. /WebApp/index.jsp fails
    This means your project's deployment context path isn't WebApp. By default, Maven uses the artifact ID (app) as the context path unless configured otherwise.

  3. Misconfigured ContextLoaderListener
    You're loading dispatcher-servlet.xml via the root context's ContextLoaderListener, which is incorrect. The ContextLoaderListener should load the root application context (usually applicationContext.xml), while the DispatcherServlet loads its own dedicated context (dispatcher-servlet.xml by default). This duplication can cause unexpected behavior.

Step-by-Step Fixes

Fix 1: Correct the ContextLoaderListener Configuration

Edit your web.xml to remove the context-param and listener (since your applicationContext.xml is empty right now), or point the context-param to applicationContext.xml if you plan to use it later. Here's the cleaned-up version:

<?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_3_1.xsd" version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Fix 2: Handle Static Resources Properly

To let the server serve static files like .jsp directly, add <mvc:default-servlet-handler/> to your dispatcher-servlet.xml. This tells Spring MVC to pass static resource requests to the container's default servlet instead of trying to handle them with controllers:

<?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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:annotation-driven />
<mvc:default-servlet-handler/> <!-- Add this line -->
<context:component-scan base-package="test.app" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value = "/WEB-INF/views/"/>
<property name="suffix" value=".jsp" />
</bean>
</beans>

Fix 3: Verify JSP File Location

Ensure your index.jsp is in the right spot based on your ViewResolver:

  • If accessing via the root path (controller), it should live at WEB-INF/views/index.jsp
  • If wanting direct access via /index.jsp, place it in the project's root directory (outside WEB-INF)

Fix 4: Set the Correct Deployment Context Path

If you want to access your app at /WebApp, configure the Maven War Plugin to set the war name (which becomes the context path). Add this to your pom.xml inside the <project> tag:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.2</version>
            <configuration>
                <warName>WebApp</warName>
            </configuration>
        </plugin>
    </plugins>
</build>

After making these changes, rebuild your project and redeploy it to Tomcat. You should be able to access both the root path and direct .jsp requests without 404 errors.


内容的提问来源于stack exchange,提问作者reagan yuan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:58:07