使用Maven搭建SpringMVC时出现Error 404问题求助
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:8080works fine and shows your "HELLO WORLD" page - Directly visiting
http://localhost:8080/index.jsporhttp://localhost:8080/WebApp/index.jspthrows a 404 error - Renaming the controller method from
index()totest()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:
Direct
index.jspaccess fails
Your DispatcherServlet is mapped to/, meaning it handles all incoming requests—including static files like.jsp. When you hithttp://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 yourTestControllermaps to/and returns "index", which your ViewResolver resolves to/WEB-INF/views/index.jsp./WebApp/index.jspfails
This means your project's deployment context path isn'tWebApp. By default, Maven uses the artifact ID (app) as the context path unless configured otherwise.Misconfigured ContextLoaderListener
You're loadingdispatcher-servlet.xmlvia the root context'sContextLoaderListener, which is incorrect. TheContextLoaderListenershould load the root application context (usuallyapplicationContext.xml), while the DispatcherServlet loads its own dedicated context (dispatcher-servlet.xmlby 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 (outsideWEB-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

