i have a table which have many columns. When i update some of the columns using @ModelAttribute, the data from the columns which are not specified in jsp page is removed.
example
@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:/";
}
the problem is that i am not binding all the columns in this update and thus only bound columns data is left and other columns data is removed from the database. I have like 15 columns and i am binding 5 columns in this jsp page.
My full Controller Code :
@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:/";
}
My Student model id large and i am updating it by 2 controllers. Student profile in different jsp page and education in different jsp page with different controllers. When i update one portion the other portion deletes.