Spring redirection guide and related policy questions, springredirect

Source: Internet
Author: User

Spring redirection guide and related policy questions, springredirect

Overview

This article will focus on implementing Redirect in Spring and discuss the reasons behind each policy.

Why is redirection required?

Let's first consider why you may need to perform a redirection in Spring applications.

Of course there are many possible examples and causes. A simple case may be POST form data, which involves dual-commit issues, or simply entrusting the execution stream to another Controller method.

Note that the typical Post/Redirect/Get mode does not fully address the dual commit problem-the problem of refreshing the page before the initial commit is complete may still cause a dual commit.

Use RedirectView redirection

Let's start with this simple method-Let's look at an example:

RedirectView triggers HttpServletResponse. sendRedirect ()-This will execute the actual redirection.

Note: here we inject the redirection attributes into the method-the Framework completes this heavy work and enables us to interact with these attributes.

We add attribute to the model RedirectAttributes-expose it as an HTTP Query parameter. Objects contained in this model-usually strings or objects that can be converted to strings.

Now let's test our redirection function-use a simple curl command to help implement:

The result will be:

Use redirect: prefix for redirection

The previous method uses RedirectView because it is not optimal for some reasons.

First, we are now coupled with Spring API, because we directly use RedirectView in our code.
Secondly, we need to know from the very beginning that when the Controller operation is implemented, its results will always be redirected, but this is not always the case.

A better choice is to use redirect: prefix -- The redirect view name is injected into the Controller like other logical view names. The controller does not even know that the redirection is happening.

It looks like this:

When the view name is returned together with redirect:, the UrlBasedViewResolver class (and all its subclasses) recognizes it as a special indicator for redirection. The rest of the view name is treated as a Redirection URL.

Note that when we use the redirect:/redirectedUrl logic view here, we are doing a redirection related to the current Servlet context.

If you need to redirect to an absolute URL, you can use a name like this: redirect: http: // localhost: 8080/spring-redirect/redirectedUrl.

So now, when we execute the curl command:

We will immediately get a redirection:

Forward with a forward prefix

Now let's look at how to do something slightly different-a forward.

Before reading the code, let's take a look at the fast and high-level summary of the semantics of forwarding and redirection:

Redirection will respond with a new URL containing the 302 response code and Location header; then the browser/client will send a request to the new URL again
Forwarding occurs 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 look at the Code:

Like redirect:, the forward: prefix is parsed by UrlBasedViewResolver and its subclass. Internally, this creates an InternalResourceView, which executes a RequestDispatcher. forward () operation for the new view.

When we use curl to execute this command:

We will get HTTP 405 (the method is not allowed ):

Compared with the two requests in the redirection solution, in this case, we only have one request sent from the browser/client to the server. Of course, the attributes previously added by redirection are not required.

Attributes that contain RedirectAttributes

Next-let's take a look at passing attributes in a redirection-make full use of the RedirectAttribures in the framework:

As mentioned above, We can insert property objects directly in the method-This makes this mechanism very easy to use.

Note that we also add a Flash Attribute-this is an attribute that will not be added to the URL. We can use this attribute to implement -- we can use @ ModelAttribute ("flashAttribute") in the final target method of redirection later to access the flash attribute:

Therefore, it is completed successfully-if you need to use curl to test this function:

We will be redirected to a new location:

In this way, replacing ModelMap with RedirectAttribures gives us the ability to share some attributes only between the two methods involved in the redirect operation.

Another configuration without a prefix

Now let's explore another configuration-redirection without a prefix.

To achieve this, we need to use org. springframework. web. servlet. view. XmlViewResolver:

Replace the org. springframework. web. servlet. view. InternalResourceViewResolver we used in the previous Configuration:

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 is:

Redirect http post Request

For use cases like bank payments, we may need to redirect http post requests. Based 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 code 301 (permanently removed) and 302 (found) allow the request method to be changed from POST to GET. This specification also defines the 307 (temporary redirection) and 308 (permanent redirection) status codes that are not allowed to change the request method from POST to GET.

Now let's look at the code that redirects a post request to another post request:

Now, let's use the curl command to test the redirected POST:

We are redirecting to the target address:

Conclusion

This article describes three different methods for implementing redirection in Spring, how to process/pass attributes and how to handle http post request redirection when executing these redirection.

The above is a small series of VSpring redirection (Redirect) guide and related policy questions, I hope to help you, if you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.