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
You have 3 options
@SessionAttributes
to store the object in the session in between requests.@ModelAttribute
annotated method to retrieve the object before each request@SessionAttributes
annotation to your controller classSessionStatus
as a parameter to your update method and the setComplete()
method when you are finished with the object@SessionAttributes("emp")
public class EmployeeController {
@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
SessionStatus status) {
// update changes in DB
status.setComplete();
}
}
@ModelAttribute
showEmpDetails
method as it should only return a view namepublic class EmployeeController {
@ModelAttribute("emp")
public Employee getEmployee(@RequestParam("empdId") Long id) {
// Get employee using empId from DB
return emp;
}
@RequestMapping(value = "/showEmpDetail.html", method = RequestMethod.GET)
public String showEmpDetails() {) {
return "showEmpDetail";
}
}
HttpSession
as an argumentshowDetails
method next to adding it to the model add it to the sessioneditEmpFormSubmission
use the one from the session and copy all non-null fields to the object from the session and store that in the database.I wouldn't go for option, I strongly would suggest option 1 especially including the setComplete()
on the SessionStatus
object for cleanup. You could also combine 1 and 2 (have a @ModelAttribute
annotated method and still use @SessionAttributes
.).
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
You could make a field in the object that is filled with a random number at the time of initial render and then store that object in the session. In the view you can map that field with a hidden input and now when user sends a request to edit action you can get that hidden field and fetch the original object from session by the number in the hidden field. That would resolve the multiple edits in different tabs conflict.