What's the diference between "return new ModelAndView("redirect:surveys.html");" and "return new ModelAndView("surveys.html");"

StackOverflow https://stackoverflow.com/questions/22091765

Question

The question is in the title. I got a Spring MVC Web App and I have to modify many things, I'm noob with this and before do anything I'm trying to understand how is made.

What's the difference between:

return new ModelAndView("redirect:surveys.hmtl");

and

return new ModelAndView("surveys.html");

Thanks.

Was it helpful?

Solution

The first one redirects:

                  POST or GET
browser -------------------------------------> spring controller

         redirect to surveys.html (status = 302)
        <------------------------------------

                  GET
        -------------------------------------> surveys.html

                  final page
        <-------------------------------------

The second one forwards:

                    POST or GET
browser -------------------------------------> spring controller
                                                     |
                                                     |
                    final page                       V
        <------------------------------------- surveys.html

OTHER TIPS

Redirect - sends a http 302 Redirect to the client. And then the client will send a new request to the server, with the given url.

while return new ModelAndView("surveys.html"); instruct spring to return this view to the client

From programmer perspective it is that

return new ModelAndView("redirect:surveys.hmtl"); when you use redirect you redirect your controller to another that is applicable for redirected url. Simply speaking you can have controller that handle that path. And that controller will be fired. And can do a redirection to another page. And then in HTTP response you can see redirected status 3xx not 200 as in the normal case.

return new ModelAndView("surveys.html");

When you not use redirect you only invoke View part and display your html. There is in http response 200/OK status.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top