我刚刚开始阅读春天。我得到org.springframework.web.bind.ServletRequestDataBinder无法强制转换为org.springframework.web.bind.support.WebRequestDataBinder问题。
异常
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.web.bind.ServletRequestDataBinder cannot be cast to org.springframework.web.bind.support.WebRequestDataBinder
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:625)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:536)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因
java.lang.ClassCastException: org.springframework.web.bind.ServletRequestDataBinder cannot be cast to org.springframework.web.bind.support.WebRequestDataBinder
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:754)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:744)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:296)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:163)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:354)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:342)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:763)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:709)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:613)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:536)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
这是代码
学生豆
public class Student {
private int age,id;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
StudentController
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class StudentController {
@RequestMapping(value="/student",method=RequestMethod.GET)
public ModelAndView student(){
return new ModelAndView("student","command",new Student()); // (view name, logical name to access in view, object model)
}
@RequestMapping(value="/addstudent",method=RequestMethod.POST)
public String addStudent(@ModelAttribute("Student")Student student,ModelMap model){
model.addAttribute("name",student.getName());
model.addAttribute("age",student.getAge());
model.addAttribute("id",student.getId());
return "result";
}
}
Student.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC form Handling</title>
</head>
<body>
<h2>Student Information</h2>
<form:form method="POST" action="/SpringFormHandling/addstudent">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td><form:label path="id">Id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
当我尝试localhost:8080 / MyApp / student时,返回student.jsp页面,在提交表单后,它应该重定向到result.jsp并显示相同的填充数据,但是当我提交表单时,它会抛出ClassCastException。我已经尝试了很多没有任何线索的东西。有完整的示例https://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm
答案 0 :(得分:1)
这通常发生在类路径上有同一个类的多个版本时(例如,我在Apache Tiles中遇到了一些问题,它添加了一些旧版本的Spring MVC)。
最好检查一下您的webapp lib文件夹中的哪些罐子。或者,使用IDE(如Eclipse),您可以显示&#34; Open Type&#34;对话框并输入一个类&#39;如果它们存在,它将显示多个版本。