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:
First, dive into your web.xml and NikiAbb-servlet.xml:
- In
web.xml, confirm yourDispatcherServletis 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 whereAddController.javalives. If your controller is incom.nikiabb.controller, the scan should look like this:
If this path is wrong, Spring won’t detect your controller at all, leading to 404s.<context:component-scan base-package="com.nikiabb.controller"/>
Open up AddController.java and verify:
- The
@RequestMappingannotations match the URL you’re trying to access. For example, if your controller has:
Your full access URL should be@Controller @RequestMapping("/cart") public class AddController { @RequestMapping("/addProduct") public String showAddProductPage() { return "ViewAddproduct"; } }http://localhost:8080/[your-project-name]/cart/addProduct, not just/addProductor 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.jspvsviewaddproduct.jspare treated as different files.
Your pom.xml and resulting WAR file need to be correctly structured:
- Check that
<packaging>war</packaging>is set inpom.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
InternalResourceViewResolverinNikiAbb-servlet.xmlwith a prefix like/WEB-INF/views/, yourViewAddproduct.jspmust be insideWEB-INF/views/(not directly inWEB-INFor 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.
- JSPs are in the right location. If you’re using
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 inWEB-INF—you have to go through your controller mappings instead.
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.xmlsets the correct compiler source/target:<properties> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> </properties>
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

