这@modelattribute("studentId") Long studentId
是问题的根源,因为spring找不到可以提供此Long
对象的方法,因此它试图实例化一个对象并将其作为方法参数传递。 您可以:
@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";
}
@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实例可以从哪里来?有几种选择:
如果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";
}