Flash Attributes
Explore the concept of flash attributes stored temporarily before a redirect and removed after they’ve been used.
The need for flash attributes
The delete works nicely, but the usability could be improved. The modal just closes without any indication that the user was actually removed. It would be a lot better to show a message after the redirect to the list of users. Something like “User Francine Connely was deleted.”
What are flash attributes?
Spring MVC has the concept of flash attributes. These attributes can be added in the controller’s POST
and will be made available in the GET
method following the redirect we do at the end of the @PostMapping
method.
Adding flash attributes
To add flash attributes, we need to inject an instance of the
org.springframework.web.servlet.mvc.support.RedirectAttributes
interface into our controller method:
Get hands-on with 1200+ tech skills courses.