我有一个有很多列的表。当我使用@ModelAttribute更新某些列时,jsp页面中未指定的列中的数据将被删除。
示例
@RequestMapping(value= "/studenteducation", method= RequestMethod.GET)
public String setupUpdateEducationForm(Model model, @ModelAttribute("education") Student student){
student = studentService.getStudent(getStudentName());
model.addAttribute("education", student); //adding saved student data into model
return "studenteducation";
}
@RequestMapping(value= "/studenteducation", method= RequestMethod.POST)
public String studentEducationForm(Model model, @ModelAttribute("education") Student student, BindingResult result){
if(result.hasErrors()){
return "studenteducation";
}
student.setStudentId(getStudentName()); // inserting primary key
studentService.updateStudent(student); // updating student table
return "redirect:/";
}
问题是我没有绑定(bind)此更新中的所有列,因此只留下绑定(bind)的列数据,而其他列数据则从数据库中删除。我有大约 15 列,我在此 jsp 页面中绑定(bind) 5 列。
我的完整 Controller 代码:
@Controller
@RequestMapping("/user")
@SessionAttributes("education")
public class StudentController {
@Autowired
StudentService studentService;
Student student = new Student();
// Setup new Student form------------------------------------------------------------------
@RequestMapping(value= "/newuser", method= RequestMethod.GET)
public String setupAddStudentForm(Model model, @ModelAttribute("student") Student student){
return "registerstudent";
}
// Add new Student------------------------------------------------------------------
@RequestMapping(value= "/newuser", method= RequestMethod.POST)
public String sddStudent(Model model, @ModelAttribute("student") Student student, BindingResult result, SessionStatus sessionStatus){
if(result.hasErrors()){
return "registerstudent";
}
studentService.addStudent(student);
sessionStatus.setComplete();
return "redirect:/";
}
// Setup Edit Profile Form------------------------------------------------------------------
@RequestMapping(value= "/updateprofile", method= RequestMethod.GET)
public String setupStudentProfileForm(Model model, @ModelAttribute("profile") Student student ){
Student stu = studentService.getStudent(getStudentName());
model.addAttribute("name", student.getStudentId());
model.addAttribute("studentprofile", stu);
return "studentprofile";
}
// Update student profile------------------------------------------------------------------
@RequestMapping(value= "/updateprofile", method= RequestMethod.POST)
public String studentProfileForm(Model model, @ModelAttribute("profile") Student student, BindingResult result, SessionStatus sessionStatus ){
if(result.hasErrors()){
return "studentprofile";
}
student.setStudentId(getStudentName());
studentService.updateStudent(student);
sessionStatus.setComplete();
return "redirect:/";
}
// Setup Update Education Form------------------------------------------------------------------
@RequestMapping(value= "/studenteducation", method= RequestMethod.GET)
public String setupUpdateEducationForm(Model model, @ModelAttribute("education") Student student){
student = studentService.getStudent(getStudentName());
model.addAttribute("education", student);
return "studenteducation";
}
// Update Student Education------------------------------------------------------------------
@RequestMapping(value= "/studenteducation", method= RequestMethod.POST)
public String studentEducationForm(Model model, @ModelAttribute("education") Student student, BindingResult result, SessionStatus sessionStatus){
if(result.hasErrors()){
return "studenteducation";
}
student.setStudentId(getStudentName());
studentService.updateStudent(student);
sessionStatus.setComplete();
return "redirect:/";
}
我的学生模型 ID 很大,我正在通过 2 个 Controller 更新它。不同jsp页面中的学生资料和具有不同 Controller 的不同jsp页面中的教育。当我更新一部分时,另一部分会删除。
最佳答案
这是 Spring MVC 和 Hibernate 协同工作的方式。当您在 Controller 方法中编写 @ModelAttribute("education") Student Student
时,Spring MVC 会创建一个新对象并用表单数据填充它。所有其他字段保留其初始化值(通常为空)。
你可能会觉得这很愚蠢,但我从未在 Spring MVC 中找到一种好的、干净的方法来处理这个问题。常见的解决方法:
- 按照 Neil McGuigan 的建议使用
SessionAttributes
。问题在于,当您不再需要该属性时,它可以保留在 session 中,并且它显然不能用于无 session API 使用。 - 在 URL 中传递 ID,并使用
Converter
将其转换为正确的Student
对象。但你必须在服务层之外调用持久层。
关于java - 更新表会删除jsp页面中未绑定(bind)的列中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29019431/