What happens is that I have a jsp view from which I fill out a form, send the data through the POST method to my controller and once the instructions are finished, I return to the main view of my site. However, the methods contained in my GET controller for the main view did not start, returning it completely clean with no information, this usingreturn"vista.jsp";
Now, using return:"redirect:vista.jsp"
my methods they go smoothly returning the information, I managed to solve my error but I want to understand its difference well and that I am somewhat confused.
If you use
return "vista";
the server it will perform a forward on sight. If you usereturn "redirect:vista";
the server it will perform a redirect to the url associated with the view. With this in mind, your question translates to what are the differences between a forward and a redirect. These concepts are not associated with Spring but with the development of Web applications in Java.forward
Your request will return a response with a code 200, 201 or another, for example 500 if an error occurs during the attention of the request. In this case, the content of the response will be the content of your view, that is, your JSP. The benefits are that you can reuse the attributes put in the request scope for use in rendering the view. That is, by using Forward you can do this:
Code in controller:
Code in the view (usually JSP):
Redirect
Your request will return a response with a 300 HTTP code indicating that the client (the browser) has to make a new request to another url on the server (or perhaps an external server). The server will receive this request and serve it.
In this case, the attributes set at the request level cannot be used because a new request-response cycle will be generated towards the server.
What happens in your application is:
When you execute
return"vista.jsp"
you return the jsp to the browser, I guess when you sayIt is that you have a controller that manages the
request
user and puts information in yourvista.jsp
, but this controller that you show does not have that logic, that's why it comes out "empty"When you do a redirect:
A
request
browser is started towards your url, so the associated controller can load the information, since at this moment it is entering the gameSmall scheme of both cases