Currently working on a project with Spring MVC, I am looking to pass a JSON object from one page to another. My approach involves creating a controller where the object is sent via a POST AJAX call. The controller then constructs a ModelAndView object for the next jsp page, includes the object as an attribute, and returns it. Now, the challenge lies in redirecting to the ModelAndView-rendered page. See below for the relevant code snippet:
Controller:
public ModelAndView nextPageController(@RequestBody String passedJsonString) {
ModelAndView nextPage = new ModelAndView("/nextPage.jsp");
nextPage.addObject("passedJsonString", passedJsonString);
return nextPage;
}
AJAX Call:
controllerUrl = "/controller/path";
$.ajax({
type : "POST",
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
url : controllerUrl,
data : JSON.stringify(jsonDataToBePassed),
success : function(dataString) {
console.log(dataString);
}
});
The AJAX call completes successfully and the expected ModelAndView object is returned. However, now the task is to display the next page with the added controller attributes intact. I acknowledge that my current method may not be optimal, so any suggestions on achieving this more efficiently would be greatly appreciated.