Spring MVC简易Java Web项目运行出现404页面未找到问题求助
Hey there! Let's break down why you're hitting that frustrating 404 error when running your simple Java Web project. I've seen this a hundred times—usually it's one of a handful of common misconfigurations. Let's go through the checks step by step:
1. Verify Your Controller Setup
First, make sure your Controller is actually being recognized by Spring:
- Check Controller Annotations: Did you add
@Controller(for view-based responses) or@RestController(for JSON responses) to your controller class? Without these, Spring won't register it as a request handler. - Double-Check Request Mappings: Let's say your controller method has
@GetMapping("/greet")—your full request URL should behttp://localhost:8080/[your-project-context]/greet. Don't forget the project context path (usually the name of your deployed WAR file). - Ensure Package Scanning is Enabled: In your
dispatcher-servlet.xml, you need to tell Spring to scan your controller package. Add this line if it's missing:
Without this, Spring will never find your controller class.<context:component-scan base-package="controller"/>
2. Validate dispatcher-servlet.xml Configurations
This file is the heart of your Spring MVC setup—let's make sure it's correct:
- Enable Annotation-Driven Support: Add
<mvc:annotation-driven/>to enable Spring's MVC annotation processing (like@RequestMapping,@GetMapping, etc.). Without this, your mappings won't be detected. - Check View Resolver (if using JSPs): If your controller returns a view name (like
"home"), you need a view resolver to map that to the actual JSP file. Example configuration:
If your JSP is at<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean>/WEB-INF/views/home.jsp, returning"home"will work—but if the prefix/suffix is wrong, or the file doesn't exist, you'll get a 404. - Handle Static Resources: If you're loading CSS/JS/images and getting 404s for those too, add
<mvc:default-servlet-handler/>to let the server's default servlet handle static files (since your DispatcherServlet is mapped to/).
3. Check web.xml Configurations
The web.xml defines how your server handles requests—let's confirm these settings:
- DispatcherServlet Mapping: Avoid using
/*as the url-pattern (it intercepts all requests, including JSPs). Use/instead, which maps to all requests except JSPs and static resources. Example:<servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> - Confirm DispatcherServlet Initialization: Make sure you're pointing to the correct
dispatcher-servlet.xmlpath in the init-param:
If the path is wrong, the servlet won't load your configuration, and no mappings will be registered.<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/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> - Project Context Path: If you didn't set a custom context path, it's usually the name of your deployed project. So if your WAR is named
my-web-app.war, your base URL ishttp://localhost:8080/my-web-app/.
4. Check Project Deployment & Structure
- Confirm Deployment: Navigate to your Tomcat (or whatever server you're using)
webappsdirectory—your project folder/WAR should be present there. If not, re-deploy the project. - Verify File Locations: JSPs should typically be under
/WEB-INF/views/(matches the view resolver prefix), and static resources under/resources/or/static/in your web root.
Quick Debug Tip
If none of the above fixes it, check your server's logs (Tomcat's logs/catalina.out or similar). Look for errors like "Could not find controller class" or "Failed to parse dispatcher-servlet.xml"—these will point you directly to the problem.
内容的提问来源于stack exchange,提问作者Zaim Busuladzic

