Spring 4 Official Document Learning (11) Web MVC Framework Flash Attributes

Source: Internet
Author: User

Redirect in one of the previous articles.

Http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-flash-attributes

Flash attributes provides a way for a request to save attribute for other requests. This is common when redirecting--for example, post/redirect/get pattern. Flash attributes will be temporarily saved- usually saved in session before redirection.

For the support of Flash attributes, Spring MVC has two main abstractions. The flashmap is used for hold flash attributes,Flashmapmanager for storing, acquiring, and managing Flashmap instances.

Flash attribute support is always open and does not need to be explicitly enabled. Each request has an "input" flashmap-the attributes (if any) that came with the previous request, and an "output" flashmap--with the attributes that will be stored for use with the next request. Two Flashmap instances can be accessed anywhere using the static methods in the Requestcontextutils.

Controllers that use annotations usually do not need to use flashmap directly. Instead, @RequestMapping method can accept a parameter of type redirectattributes and use it to add flash attributes for redirection. Flash attributes added via redirectattributes are automatically propagated to the "output" flashmap. Similarly, after redirection, the attributes from the "input" Flashmap is automatically added to the controller's model to serve the target URL.

Match request to Flash attributes

The concept of flash attributes exists in many other web frameworks and is proven to cause concurrency problems. This is because, by definition, flash attributes is stored as the deadline to the next request. However, the next request may not be the intended recipient, but another asynchronous request (for example, a polling or resource request), in which case the flash attributes will be removed prematurely.

To reduce the likelihood of this problem, Redirectview automatically adds a stamp to the Flashmap instance-path and query parameters with the target redirect URL. The default Flashmapmanager matches the request and the information-when looking for the "input" flashmap.

This does not completely eliminate the likelihood of concurrency problems, but it can be largely reduced. Therefore, flash attributes, recommended mainly in the redirection scene use!!!

Spring 4 Official Document Learning (11) Web MVC Framework Flash Attributes

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.