这
@ModelAttribute("studentId") LongstudentId是问题的根源,因为spring找不到可以提供此Long对象的方法,因此它试图实例化一个对象并将其作为方法参数传递。
要解决此问题, 您可以:
从方法参数中删除@ModelAttribue
@RequestMapping(value = "/read.html")
public String readStudent(Model model,Long studentId) {
Student student = null;
studentId = 2l;
try {
student = serviceFile.readStudent(studentId);
} catch(Exception e){
model.addAttribute(“message”, “Some thing went wrong !!!! Exception occured”);
return “message”;
}
model.addAttribute(“student”, student);
return “read”;
}创建将
Long
在您的 控件 中 提供该 对象 的方法@ModelAttribute
public void provideStudentId(Model model){
model.addAttribute(“studentId”, new Long(1));
}
官方文件
@RequestMapping(path = "/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)public String processSubmit(@ModelAttribute Pet pet) { }给定以上示例,Pet实例可以从哪里来?有几种选择:
- 由于使用@SessionAttributes,它可能已经在模型中-
请参阅“使用@SessionAttributes在请求之间的HTTP会话中存储模型属性”一节。- 由于同一控制器中的@ModelAttribute方法,它可能已经在模型中-如上一节中所述。
- 可以基于URI模板变量和类型转换器(在下面更详细地说明)来检索它。
- 可以使用其默认构造函数实例化它。
编辑
如果studentId是参数的名字从你可以使用UI发送
@RequestParam这样
@RequestMapping(value = "/read.html")public String readStudent(Model model, @RequestParam("studentId") Long studentId) { Student student = null; studentId = 2l; try { student = serviceFile.readStudent(studentId); } catch(Exception e) { model.addAttribute("message", "Some thing went wrong !!!! Exception occoured"); return "message"; } model.addAttribute("student", student); return "read";}


