Spring MVC中@RequestMapping无法映射URL的问题求助
Hey there! Let's figure out why your @RequestMapping annotations aren't working—this is a super common pitfall when starting with Spring MVC, so don't worry, we'll fix it quickly.
The Root Cause
Spring web apps have two distinct contexts, and you mixed up where your web-layer config goes:
- Root Context (loaded by
ContextLoaderListener, tied toapplicationContext.xml): This is for non-web beans like service layers, data sources, and transaction managers. - DispatcherServlet Context (tied to
dispatcherServlet.xml): This is exclusively for web-layer components—controllers, view resolvers, and the logic that handles@RequestMappingmappings.
You put your component scan and annotation config in the root context, so even though your controller beans are being created, the DispatcherServlet can't see them to map their @RequestMapping paths.
Step 1: Fix Your Context Configs
Let's split the config correctly between the two files.
Update applicationContext.xml
Remove the annotation and component scan lines—this file should only handle non-web stuff:
<?xml version='1.0' encoding='UTF-8' ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- Keep your persistence/datasource config here if you add it later --> <!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties" /> --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> --> </beans>
Update dispatcherServlet.xml
Add the context and mvc namespaces, enable annotation-driven Spring MVC, and add component scanning for your controller package:
<?xml version='1.0' encoding='UTF-8' ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- Enable Spring MVC annotation support (critical for @RequestMapping) --> <mvc:annotation-driven/> <!-- Scan only your controller package to avoid duplicate bean creation --> <context:component-scan base-package="com.company.product.controller"/> <!-- Keep your existing index mapping and view resolver --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController" p:viewName="index" /> </beans>
Step 2: Fix Your Controller Class
Your MyController.java code was cut off, so make sure it has the correct annotations:
- Add
@Controllerto the class (this tells Spring it's a web controller) - Make sure your
@RequestMappingpaths match your DispatcherServlet's URL pattern (*.htm)
Here's a complete example:
package com.company.product.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class MyController { @RequestMapping("/hello.htm") public ModelAndView showHelloPage() { ModelAndView mav = new ModelAndView("hello"); // Maps to /WEB-INF/jsp/hello.jsp mav.addObject("greeting", "Hey there, Spring MVC is working!"); return mav; } }
Step 3: Quick Cleanup Tips
- Remove
ControllerClassNameHandlerMappingfrom your dispatcher config—<mvc:annotation-driven/>already includes the necessary handler mappings for annotations, and the old class-based mapping can cause conflicts. - Double-check your dependencies: Ensure you have the
spring-webmvc4.0.1 JAR in your project (NetBeans should handle this if you set up the Spring MVC project correctly, but it's worth verifying). - Restart your server: Config changes won't take effect until you restart the app server.
After making these changes, fire up your server and hit http://localhost:8080/your-project-name/hello.htm—you should see your hello page load, and your @RequestMapping will be working as expected!
内容的提问来源于stack exchange,提问作者SOLID Developper

