Maven搭建Spring MVC遇请求映射错误,寻求解决方案
Hey there, let's work through that "No mapping found for HTTP request" error you're hitting with your Spring MVC app. I’ve gone through your code, and here are the key issues and fixes to get things working:
Core Issues & Fixes
1. Missing MVC Annotation Driver
Your subu-servlet.xml is missing the <mvc:annotation-driven /> configuration. This is critical—it tells Spring MVC to recognize and process annotations like @Controller and @RequestMapping. Without it, the DispatcherServlet can’t find your controller mappings at all.
2. Unhandled View Return Path
Right now you’re returning "display.jsp" directly from your controller, but Spring has no way to resolve this path without either a view resolver or an explicit forward/redirect instruction.
3. Mismatched Schema Versions (Optional but Recommended)
Your config file uses Spring MVC 3.0 schemas, but you’re using Spring 4.1.8. Updating the schemas to match your Spring version avoids potential compatibility hiccups.
Step-by-Step Modifications
Update subu-servlet.xml
Add the annotation driver, fix the schema versions, and optionally add a view resolver to simplify view paths later:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:ctx="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <!-- Enable annotation-based Spring MVC functionality --> <mvc:annotation-driven /> <!-- Scan for controllers in your package (no need for the wildcard *) --> <ctx:component-scan base-package="com.subu"></ctx:component-scan> <!-- Optional: Add a view resolver to avoid typing .jsp suffixes later --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
Adjust Your Controller (If Skipping the View Resolver)
If you want to skip setting up the view resolver for now, modify your controller to return an explicit forward path:
package com.subu; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class AddController { @RequestMapping("/add") public String add() { // Forward directly to your JSP (adjust path if your file is in WEB-INF) return "forward:/display.jsp"; } }
Verify Your File Placement
- If you use the view resolver above, move
display.jsptoWEB-INF/views/(create theviewsfolder if it doesn’t exist) - If using the forward path, make sure
display.jspis in your web root (same level asindex.jsp)
Quick Check for Other Potential Issues
- Confirm
subu-servlet.xmlis placed in theWEB-INFdirectory (this is where DispatcherServlet looks for it by default) - Your form’s
action="add"is correctly mapped to your controller’s@RequestMapping("/add"), so that part is good
After making these changes, rebuild and redeploy your app. Clicking the submit button should now correctly route to your controller and load display.jsp.
内容的提问来源于stack exchange,提问作者Subhransu Sekhar Das

