On spring redirection Guide
1. General
This article will focus on implementing redirect in spring and discuss the reasons behind each strategy.
2. Why redirect?
Let's first consider why you might need to do a redirect in a spring application.
Of course, there are many possible examples and reasons. A simple might be post form data, around double submission issues, or simply delegate the execution flow to another controller method.
Note: the typical post / redirect / get mode does not fully solve the problem of double submission - the problem of refreshing the page before the initial submission is completed may still lead to double submission.
3. Redirect using redirectview
Let's start with this simple method - let's take an example:
Behind the scenes, redirectview will trigger httpservletresponse Sendredirect () - this will perform the actual redirection.
Notice how we inject redirection attributes into methods here - the framework does this heavy work so that we can interact with these attributes.
We add attributes to the model redirectattributes - exposing them as HTTP query parameters. The objects contained in the model - are usually strings or objects that can be converted into strings.
Now let's test our redirection function - with a simple curl command to help implement:
The result will be:
4. Redirect using redirect: prefix
The previous method uses redirectview, which 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 the controller operation is implemented, its result 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 does not even know that redirection is taking place.
It looks like this:
When the view name is returned with redirect:, the urlbasedviewresolver class (and all its subclasses) will recognize it as a special indication that redirection is required. The rest of the view name will be treated as a redirection URL.
There is one thing to note here - when we use the redirect: / redirectedurl logical view here, we are doing a redirect related to the current servlet context.
If we 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'll get a redirect immediately:
5. Forward with forward prefix:
Let's now look at how to do something slightly different -- a forwarding.
Before looking at the code, let's take a quick and high-level overview of the semantics of forwarding and redirection:
Now let's look at the code:
Like redirect:, the forward: prefix will be resolved by the urlbasedviewresolver and its subclasses. Internally, this creates an internalresourceview, which executes a requestdispatcher for the new view Forward() operation.
When we execute this command with curl:
We will get HTTP 405 (method not allowed):
Compared with the two requests in our redirection solution, in this case, we only have one request sent from the browser / client to the server. Of course, the properties previously added by redirection are no longer required.
6. Attributes containing redirectattributes
Next - let's look at passing attributes in a redirection - make full use of redirectattribures in the framework:
As mentioned earlier, we can insert attribute objects directly into methods - which makes the mechanism very easy to use.
Also note that we also add a flash attribute - an attribute that will not be added to the URL. We can achieve this through this attribute -- we can use @ modelattribute ("flashattribute") to access the flash attribute later in the method of the ultimate goal of redirection:
So, it's done -- if you need to test the function with curl:
We will be redirected to a new location:
In this way, using redirectattribures instead of modelmap gives us the ability to share some attributes only between the two methods involved in the redirection operation.
7. Another configuration without prefix
Now let's explore another configuration -- redirection without a prefix.
To do this, we need to use org springframework. web. servlet. view. XmlViewResolver:
Instead of org. Org we used in the previous configuration springframework. web. servlet. view. InternalResourceViewResolver:
We also need to define a redirectview bean in the configuration:
Now we can reference this new bean by ID to trigger redirection:
To test it, we use the curl command again:
The result would be:
8. Redirect HTTP post request
For use cases like bank payment, we may need to redirect HTTP post requests. According to 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 the request method to be changed from post to get. Now, let's look at the code that redirects the post request to another post request: