示例
@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:/";
}
问题是我没有绑定此更新中的所有列,因此只保留绑定列数据,并从数据库中删除其他列数据。我喜欢15列,我在这个jsp页面中绑定了5列。
我的完整控制器代码:
@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个控制器更新它。不同jsp页面中的学生档案和不同jsp页面中具有不同控制器的教育。当我更新一部分时,另一部分会删除。
答案 0 :(得分:1)
这是@SessionAttributes的用途
在Controller类声明
之上添加@SessionAttributes("education")
在第一种方法中删除@ModelAttribute("education") Student student
参数。改为以该方法将学生添加到模型中。
在POST处理程序方法中添加SessionStatus
参数,并在完成后调用sessionStatus.setComplete()
。
您应该使用Post-Redirect-Get模式。
如果用户不应该编辑此表单中的某些实体字段,您还应该使用@InitBinder来设置不允许的字段:
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("foo");
}
答案 1 :(得分:1)
这是Spring MVC和Hibernate协同工作的方式。当您在控制器方法un @ModelAttribute("education") Student student
中编写时,Spring MVC会创建一个新对象并使用表单数据填充它。所有其他字段保留其初始化值(通常为null)。
你可能会发现它很愚蠢,但我从未在Spring MVC中找到一个很好的和干净的方法来处理它。常见的解决方法:
SessionAttributes
。问题是,当您不再需要该属性时,该属性可以保持在会话中,并且它显然不能用于无会话API使用。Converter
将其转换为正确的Student
对象。但是你必须在服务层之外调用持久层。