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

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:

1. Double-Check Eclipse & Tomcat Deployment Setup

First, make sure your project is actually being deployed correctly to Tomcat:

  • Open the Servers view in Eclipse (go to Window > Show View > Servers if it’s hidden). Right-click your Tomcat 8.0.48 instance and confirm your Spring MVC project is listed under Modules. If not, click Add and Remove... to move your project to the "Configured" side.
  • Verify the deployment assembly: Right-click your project > Properties > Deployment Assembly. Ensure Maven Dependencies is included here—if it’s missing, add it by selecting Add > Java Build Path Entries > Maven Dependencies. This ensures all your Spring jars get deployed to Tomcat (missing dependencies often cause silent failures).
2. Fix Your pom.xml Dependencies

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-webmvc is one of the most common causes of 404s—this jar contains the DispatcherServlet that routes requests to your controllers.

3. Validate DispatcherServlet Configuration

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 WebApplicationInitializer class is correctly registering the DispatcherServlet and mapping it to all requests.
4. Check Controller & View Setup

Make sure your controller and views are configured properly:

  • Your controller class must have the @Controller annotation, 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>
5. Dig Into Tomcat Logs

If none of the above fixes work, check Tomcat’s logs for hidden errors:

  • In Eclipse, double-click your Tomcat instance in the Servers view, then click Open launch configuration > Arguments > Working directory to find the log path.
  • Look at localhost.log for deployment errors (like missing config files or jar conflicts) and localhost_access_log.* to confirm requests are hitting Tomcat at all.
6. Confirm Your File Structure

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:10:40