java.lang.IllegalStateException:在@RequestMapping中找不到@PathVariable [pathVars]
如何将Spring 4.1.6 RELEASE版本更新到我的项目?
@Controller
public class HelloController {
@RequestMapping("/welcome/{countryName}/{userName}")
public ModelAndView helloWorld(@PathVariable Map < String, String > pathVars) {
String name = pathVars.get("userName");
String country = pathVars.get("countryName");
ModelAndView modelAndView = new ModelAndView("HelloPage");
modelAndView.addObject("msg", "Hello " + name + "You are from" + country);
return modelAndView;
}
这是我的spring-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.learningspringmvc.controller"></context:component-scan>
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
答案 0 :(得分:0)
您尝试使用的PathVariableMapMethodArgumentResolver
存在于Spring 4.1.6 RELEASE中,因此您的代码应该有效。
您可能没有启用Spring Web MVC。
您需要在xml配置中加入<mvc:annotation-driven/>
,或使用@EnableWebMvc
。