Ubuntu环境下Eclipse搭建Spring MVC Maven项目遇404错误求助
Hey there, let's troubleshoot that frustrating 404 error you're facing with your Spring MVC setup. I’ve dealt with plenty of similar issues using Ubuntu 16.04, Eclipse Neon, and Tomcat 8.x, so let’s walk through the most likely fixes step by step:
First, make sure your project is actually being deployed correctly to Tomcat:
- Open the
Serversview in Eclipse (go toWindow > Show View > Serversif it’s hidden). Right-click your Tomcat 8.0.48 instance and confirm your Spring MVC project is listed under Modules. If not, clickAdd and Remove...to move your project to the "Configured" side. - Verify the deployment assembly: Right-click your project >
Properties > Deployment Assembly. EnsureMaven Dependenciesis included here—if it’s missing, add it by selectingAdd > Java Build Path Entries > Maven Dependencies. This ensures all your Spring jars get deployed to Tomcat (missing dependencies often cause silent failures).
You mentioned having the spring-context jar, but Spring MVC requires a few more core dependencies to work properly. Here’s what your pom.xml should include (adjust versions to match your tutorial if needed):
<dependencies> <!-- Critical Spring MVC core jar (includes DispatcherServlet) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.8.RELEASE</version> </dependency> <!-- Your existing spring-context dependency --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.8.RELEASE</version> </dependency> <!-- Servlet API (marked as provided since Tomcat includes it) --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- JSTL for JSP view rendering (if your tutorial uses JSPs) --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> </dependencies>
Note: Omitting
spring-webmvcis one of the most common causes of 404s—this jar contains the DispatcherServlet that routes requests to your controllers.
The DispatcherServlet is the heart of Spring MVC; if it’s misconfigured, requests won’t reach your controllers.
- If you’re using XML config (via
web.xml), ensure it looks like this:
<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> <!-- Loads servlet on server start --> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> <!-- Maps ALL requests to the DispatcherServlet --> </servlet-mapping>
- If you’re using Java-based config, confirm your
WebApplicationInitializerclass is correctly registering the DispatcherServlet and mapping it to all requests.
Make sure your controller and views are configured properly:
- Your controller class must have the
@Controllerannotation, and handler methods need valid request mappings. Example:
@Controller public class HomeController { @RequestMapping("/") // Maps the root URL to this method public String showHomePage() { return "home"; // Matches a view file (e.g., /WEB-INF/views/home.jsp) } }
- Verify your view resolver in
dispatcher-servlet.xml(or Java config) points to the correct view location. For JSPs:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean>
If none of the above fixes work, check Tomcat’s logs for hidden errors:
- In Eclipse, double-click your Tomcat instance in the
Serversview, then click Open launch configuration > Arguments > Working directory to find the log path. - Look at
localhost.logfor deployment errors (like missing config files or jar conflicts) andlocalhost_access_log.*to confirm requests are hitting Tomcat at all.
Ensure your project follows the standard Maven web app layout—misplaced files will break Spring’s ability to find controllers or views:
src/ ├── main/ │ ├── java/ │ │ └── com/yourpackage/controller/ │ │ └── HomeController.java │ ├── resources/ │ └── webapp/ │ ├── WEB-INF/ │ │ ├── dispatcher-servlet.xml │ │ ├── web.xml │ │ └── views/ │ │ └── home.jsp │ └── index.html (optional) └── pom.xml
内容的提问来源于stack exchange,提问作者beer

