Spring redirection request to transfer data across the

Summary

In the development scenario, most of the data request is used to forward (Forward) can be passed, and redirection (the redirect) the data transfer may be less.
So the question is: data lifecycle request survival time of only forwarding (request) in a request, and when the end of this request, the data carried by then the request will worship together with this request. The redirect initiates two requests to the server, so the data is not the first request to the second request would not it? Figure:

If we want to pass the data are available under the second request, then how to do it?
There are two ways to solve:

url transmission path
using flash property


url transmission path

url transmission path is a relatively simple way of an alternative, as various redirection and forwarding the request, it must be preceded by the redirection redirect:(without then forwards the request to the default on):
The following is redirected to colablogthe next path, passing {username}parameter: follows:

    // 如 "redirect:/colablog/johnson"
    return "redirect:/colablog/{username}" 

Another way is to use a template to define a way to redirect the URL, such as:

    @GetMapping("/red")
    public String redirect(Model model) {
        User user = ...;
        model.addAttribute("username", user.getUsername());
        return "redirect:/colablog/{username}";
    }

If user.getUsername()as johnson, then redirect url will become redirect:/colablog/johnson.


The use of flash properties

Can be found using the url transfer are some of the more simple data, when we need to pass an object, it had better how to do it? Spring provides a data transmission function for the flash, flash attributes will always carry these data until the next request and then will disappear. To provide a method for the realization of RedirectAttributesthe addFlashAttributemethod. as follows:

    @GetMapping("/test")
    public String test(RedirectAttributes attributes){
        User user = ...;
        attributes.addFlashAttribute("user", user);
        return "redirect:/colablog";
    }

Remains as extraction data, image data acquisition request forwarding (Forward) above.

    @GetMapping("/colablog")
    public String colaBlog(Model model) {
        User user = model.getAttribute("user");
        return "success";
    }

RedirectAttributesThere are Modelclasses for all methods, as RedirectAttributesis Modelthe extension of the class.

public interface RedirectAttributes extends Model {}

As for why the use of flash properties will be carried to the next request, then it will disappear? Since the data will be stored in the flash attribute among the session, after redirection, the presence of flash session attributes will be removed from the session transfer data into the model data. As shown below:

Well, the article to end here, do not know you did not read the small partners. If there are issues may in the comments below, Thanks ♪ (· ω ·) Techno

References: "the Spring actual 4th Edition"

Personal blog URL: https://colablog.cn/

If my articles help to you, I can focus on the public micro-channel number, the first time to share your article

Micro-channel public number

Guess you like

Origin www.cnblogs.com/Johnson-lin/p/12038333.html