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

Spring MVC小型购物车项目打包运行报404错误求助

Hey there, let’s work through this 404 issue together—since your individual pages run fine but the full packaged project throws an error, it’s almost certainly a configuration or routing misstep. Let’s break down the key checks to fix this:

1. Double-Check DispatcherServlet & Component Scanning

First, dive into your web.xml and NikiAbb-servlet.xml:

  • In web.xml, confirm your DispatcherServlet is mapped correctly. For Spring MVC, the standard url-pattern is / (not /*, which can cause issues with static resources). Example:
    <servlet-mapping>
        <servlet-name>NikiAbb</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
  • In NikiAbb-servlet.xml, make sure your component scan covers the package where AddController.java lives. If your controller is in com.nikiabb.controller, the scan should look like this:
    <context:component-scan base-package="com.nikiabb.controller"/>
    
    If this path is wrong, Spring won’t detect your controller at all, leading to 404s.
2. Validate Controller Request Mappings & View Names

Open up AddController.java and verify:

  • The @RequestMapping annotations match the URL you’re trying to access. For example, if your controller has:
    @Controller
    @RequestMapping("/cart")
    public class AddController {
        @RequestMapping("/addProduct")
        public String showAddProductPage() {
            return "ViewAddproduct";
        }
    }
    
    Your full access URL should be http://localhost:8080/[your-project-name]/cart/addProduct, not just /addProduct or directly accessing the JSP.
  • Ensure the returned view name (like "ViewAddproduct") exactly matches your JSP filename (including capitalization). Windows ignores case, but Tomcat (especially when deployed as a WAR) is case-sensitive—ViewAddproduct.jsp vs viewaddproduct.jsp are treated as different files.
3. Confirm Maven Packaging & WAR Structure

Your pom.xml and resulting WAR file need to be correctly structured:

  • Check that <packaging>war</packaging> is set in pom.xml—this ensures Maven builds a valid web archive.
  • Unzip your generated WAR file and verify:
    • JSPs are in the right location. If you’re using InternalResourceViewResolver in NikiAbb-servlet.xml with a prefix like /WEB-INF/views/, your ViewAddproduct.jsp must be inside WEB-INF/views/ (not directly in WEB-INF or the root). Example resolver config:
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/views/"/>
          <property name="suffix" value=".jsp"/>
      </bean>
      
    • All Spring dependencies are present in WEB-INF/lib/. Sometimes Maven skips dependencies during packaging—compare the JARs here to what’s in your local Maven repository to ensure nothing’s missing.
4. Watch the Tomcat Context Path

When deploying a WAR to Tomcat, the context path defaults to your WAR filename (unless you configure it otherwise). For example, if your WAR is named NikiAbb.war, your base URL becomes http://localhost:8080/NikiAbb/.

  • In Eclipse, you might have tested pages directly (like http://localhost:8080/ViewAddproduct.jsp), but in the packaged project, you can’t access JSPs directly if they’re in WEB-INF—you have to go through your controller mappings instead.
5. Address JDK 9 Compatibility

JDK 9’s module system can cause issues with older Spring versions:

  • If you’re using Spring 4.x, it has limited support for JDK 9. Your best bet is to either downgrade to JDK 8 (the most stable for Spring projects) or upgrade to Spring 5.x+, which fully supports JDK 9+.
  • If you stick with JDK 9, make sure your pom.xml sets the correct compiler source/target:
    <properties>
        <maven.compiler.source>9</maven.compiler.source>
        <maven.compiler.target>9</maven.compiler.target>
    </properties>
    
6. Dig Into Tomcat Logs for Clues

404 is often a symptom of an underlying issue. Check Tomcat’s logs (look in [tomcat-dir]/logs/ for localhost.log or catalina.out):

  • Look for ClassNotFoundException (indicates missing dependencies) or errors during controller initialization (means Spring couldn’t load your controller). These logs will give you a clearer picture of what’s broken beyond just the 404.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:21:51