In Spring MVC how do I pass an object between two controller methods? I have an update form and an updateController. In the controller I have 2 methods, one for fetching the data and displaying it in a view. The second method of the controller is invoked when the user clicks update button with modified changes. What I am observing is that the object which I get in second method of the controller is not the same object which I passed to the view in the first controller method call. Its a new object altogether with all form fields mapped to it. How do I make sure that the same object is passed to the second controller method which was provided to the view by the first controller method?
@RequestMapping(value = "/showEmpDetail.html", method = RequestMethod.GET)
public String showEmpDetails(
@RequestParam(value = "page", required = false) Integer page,
HttpServletRequest request, @RequestParam("empId") Long empId,
ModelMap model) {
// Get employee using empId from DB
model.addAttribute("emp",emp);
return "showEmpDetail";
}
The above controller method gets the emp values from Db and displays it correctly in the view. Now the user changes some details and clicks submit button. The following controller method is called.
@RequestMapping(value = "/editEmpFormSubmission.html", method = RequestMethod.POST)
public String editEmpFormSubmission(
@RequestParam(value = "page", required = false) Integer page,
@ModelAttribute("emp") Employee emp, BindingResult result,
ModelMap model, HttpServletRequest request) {
// update changes in DB
}
In the above controller method when I check the emp object its not the same object which I passed in previous controller call. The fields which are not form backed but had values were changed to null. How can I make sure the same object is passed by view. I do not want to add the object as sessionAttribute since a user might modify many employees in a session.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…