我只是春季MVC的新手以下是我的代码,当我尝试去bye
时出现以下错误
Could not find @PathVariable [pathVars] in @RequestMapping Spring MVC
以下是我的代码
package com.springapp.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
@Controller
public class HelloController {
@RequestMapping("/")
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello";
}
@RequestMapping("/runThis/{bye}/{hye}")
public ModelAndView printBye(@PathVariable Map<String,String> pathVars) {
String Bye = pathVars.get("bye");
String Hye = pathVars.get("hye");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", "you are"+Bye+ "AND Here COmes" +Hye+"!");
return modelAndView;
}
}
编辑完整堆栈。
type Exception report
message Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.springapp.mvc.HelloController.printBye(java.util.Map)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [pathVars] in @RequestMapping
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.springapp.mvc.HelloController.printBye(java.util.Map)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [pathVars] in @RequestMapping
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
root cause
org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.springapp.mvc.HelloController.printBye(java.util.Map)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [pathVars] in @RequestMapping
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
root cause
java.lang.IllegalStateException: Could not find @PathVariable [pathVars] in @RequestMapping
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable(AnnotationMethodHandlerAdapter.java:859)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:710)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:170)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.43 logs.
答案 0 :(得分:7)
您的dispatcher-servlet config xml
中需要<mvc:annotation-driven/>
或在春季使用的后期版本
弹簧配置类中的 @EnableWebMvc
注释
请理解@EnableWebMvc取代mvc:annotation-driven
并确保
中的xmlns正确无误<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
答案 1 :(得分:1)
而不是代码中的这个:
public ModelAndView printBye(@PathVariable Map<String,String> pathVars) {
将其更改为:
public ModelAndView printBye(@RequestParam Map<String,String> pathVars) {
这是因为@RequestParam用于处理传递给参数的Map对象,而@PathVariable用于处理传递给参数的各个项目。以下是@PathVariable:
的示例public ModelAndView printBye(@PathVariable("bye") String Bye, @PathVariable("hye") String Hye) {
答案 2 :(得分:0)
根据您的请求映射,您表明您的网址包含固定部分/runThis/
和两个可变部分{bye}
和{hye}
,并且您希望它们映射到您方法的两个参数(使用匹配的名称,因为您没有指明任何其他内容)。
但是在您的方法中,您声明了一个名为pathVars
的参数。因此,您的名称,类型和参数数量不匹配。这就是Spring MVC所抱怨的。
所以你想要的是:
@RequestMapping("/runThis/{bye}/{hye}")
public ModelAndView printBye(@PathVariable String bye, @PathVariable String hye) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", "you are" + bye + "AND Here COmes" + hye + "!");
return modelAndView;
}
它比您的代码更简单,更安全。请阅读文档。这是一个非常基本的例子。
答案 3 :(得分:0)
在某些情况下,如果ModelAndView(jspPageName)语句中的jspPageName
指向不存在的页面,您将收到相同的错误
注意我使用Spring mvc 4.2.4和JDK 8