0
    @RequestMapping(value = { "postCustomer" }, consumes = "application/json", method = {RequestMethod.POST})
    public ModelAndView postHome(@RequestBody JSONObject body, Model model) throws JSONException {
        ModelAndView mav = new ModelAndView("redirect:/feasibility");       
        return mav;
    }
    @GetMapping(value = { "feasibility" }) // GET MAPPING
    public String feasibility(ModelAndView model) throws Exception {
        return "feas";
    }

I am trying to forward/redirect the user to a new JSP file, I can go to <url>/feasibility and it updates, but when I POST postCustomer it doesn't redirect or update the view. I've also tried creating a ModelAndView with the JSP "feas" and just returning that within postCustomer but that doesn't work either. I'm not sure if I am missing something obvious, or if I have to do an additional step to update the view.

If anyone has any suggestions or solutions, I'd appreciate it.

  • Redirect will be treated as a new request try to forward the request – Robert Ellis Mar 03 '19 at 05:02
  • Judging from the fact that you are using `@RequestBody` I'm assuming you are using JavaScript to send a POST request to the server. Then JavaScript will handle the result and not the browser. Either use a regular form post instead of JSON or handle the redirect on the client side in JavaScript. (It will actually redirect but the result is received by the JavaScript client). – M. Deinum Mar 03 '19 at 11:35
  • @M.Deinum good catch but even if we handle the request in ajax we can return model and the response of the ajax will be the content of the page he is returning but that view will not have the data passed from this request since he is redirecting it – Robert Ellis Mar 04 '19 at 05:06
  • Regardless the redirect is handled by the browser (silently) and the result of the ajax call is the rendered page. If configured correctly it will contain everything, but in the result of the AJAX request NOT as a redirect directly in the browser. You will have to manually replace the content in the page. – M. Deinum Mar 04 '19 at 07:39
  • @M.Deinum i know it will not go the browser as a redirect but internal redirection will happen here and it will call the next handler that is mapped and that will be the response – Robert Ellis Mar 05 '19 at 10:18

1 Answers1

0

Redirect will be treated as a new request try to forward the request

Redirect and forward are not the same things

    @RequestMapping(value = { "postCustomer" }, consumes = "application/json", method = {RequestMethod.POST})
public ModelAndView postHome(@RequestBody JSONObject body, Model model) throws JSONException {
    ModelAndView mav = new ModelAndView("forward:/feasibility");
    //data to forward
    mav.addAttribute("data", data);
    return mav;
}
Robert Ellis
  • 714
  • 6
  • 19