Overview
This article will focus on implementing redirects in Spring and discuss the reasons behind each strategy.
Why redirect?
Let's first consider why you might need to do a redirect in your Spring application.
Of course there are many possible examples and reasons. A simple one could be POST form data, around double submission of the problem, or just delegating the execution flow to another controller method.
One thing to note is that the typical Post/Redirect/Get pattern does not adequately solve the dual commit issue - the issue of refreshing the page before the initial commit is completed may still result in a dual commit.
Redirect using RedirectView
Let's start with this simple method - let's take an example directly:
Behind the RedirectView will fire HttpServletResponse.sendRedirect() - this will perform the actual redirect.
Note how we inject redirected properties into the method here - the framework does this part of the heavy work, allowing us to interact with these properties.
We add attribute to the model RedirectAttributes - expose it as an HTTP query parameter. The model contains objects - usually strings or objects that can be converted into strings.
Now let's test our redirect function - use a simple curl command to help with it:
The result will be:
Redirect using the redirect: prefix
The previous method uses RedirectView, because it is not optimal for some reasons.
First, we are now coupled to the Spring API because we use RedirectView directly in our code.
Second, we need to know from the beginning that when implementing the controller operation, its results will always be redirected, but this is not always the case.
A better option is to use the redirect:prefix - the redirect view name is injected into the controller like other logical view names. The controller doesn't even know that the redirection is happening.
It looks like this:
When the view name is returned with redirect:, the UrlBasedViewResolver class (and all its subclasses) recognizes it as a special indication that needs to be redirected. The rest of the view name will be treated as a redirect URL.
There is a place to note here - when we use the redirect:/redirectedUrl logical view here, we are doing a redirect related to the current Servlet context.
If you need to redirect to an absolute URL, we can use a name like this: redirect: http://localhost:8080/spring-redirect/redirectedUrl.
So now, when we execute the curl command:
We will get a redirect immediately:
Forwarding with forward prefix
Let's now see how to do something slightly different - a forwarding.
Before looking at the code, let’s take a look at a quick and high-level summary of the semantics of forwarding and redirection:
The redirect will respond with a new URL containing the 302 response code and Location header; then the browser/client will make a request to the new URL again forward completely on the server side; the Servlet container forwards the same request to the target URL; the URL in the browser does not need to be changed
Now let's take a look at the code:
Like redirect:, the forward: prefix will be parsed by UrlBasedViewResolver and its subclasses. Internally, this creates an InternalResourceView which performs a RequestDispatcher.forward() operation for the new view.
When we execute the command with curl:
We will get HTTP 405 (not allowed method):
In this case, we only have one request sent from the browser/client to the server side compared to the two requests we have in the redirect solution. Of course, the properties added by redirects are not needed.
Properties containing RedirectAttributes
Next - let's look at passing properties in a redirect - take advantage of RedirectAttribures in the framework:
As mentioned earlier, we can insert property objects directly into the method - which makes the mechanism very easy to use.
Also note that we also add a Flash attribute - this is a property that will not be added to the URL. We can achieve this property - we can later use @ModelAttribute ("flashAttribute") in the redirected final target method to access the flash attribute:
So, complete successfully - if you need to use curl to test the function:
We will be redirected to the new location:
In this way, using RedirectAttribures instead of ModelMap gives us the ability to share some properties only between the two methods involved in the redirect operation.
Another configuration without prefixes
Now let's explore another configuration - redirection without prefixes.
To achieve this we need to use org.springframework.web.servlet.view.XmlViewResolver:
Instead of org.springframework.web.servlet.view.InternalResourceViewResolver we used in our previous configuration:
We also need to define a RedirectView bean in the configuration:
Now we can refer to this new bean through the id to trigger the redirect:
To test it, we use the curl command again:
The result will be:
Redirect HTTP POST request Request
For use cases like bank payments, we may need to redirect HTTP POST requests. Depending on the returned HTTP status code, the POST request can be redirected to HTTP GET or POST.
According to the HTTP 1.1 protocol reference, status codes 301 (permanently removed) and 302 (found) allow the request method to be changed from POST to GET. The specification also defines the relevant 307 (temporary redirection) and 308 (permanent redirection) status codes that do not allow changing the request method from POST to GET.
Now let's take a look at the code for redirecting the post request to another post request:
Now, let's use the curl command to test the redirected POST:
We are being redirected to the destination address:
in conclusion
This article introduces three different ways to implement redirects in Spring, how to handle/pass attributes when performing these redirects and how to handle redirections of HTTP POST requests.
The above is the VSpring Redirection (Redirect) guide and related strategy issues introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!