Flash Attributes in Spring MVC 3.1

转载 2015年07月09日 11:51:57

Spring 3.1 has added new feature called flash attributes. This is one of the most wanted feature from the spring developers. This solves the problem occured when POST/Redirect/Get pattern in the request. When user makes a request to the server via POST method, once the process is completed, the requested resourced will be served through either forward/redirect to the appropriate view. The problem with the forward is, when the user does F5 or Refresh the same page, the POST data is re-submitted because the request is same.

When we are using redirect, the forms are not re-submitted and only the final view is displayed. The real problem here is, when we invoke redirect, it loses all the request values which would be useful in the subsequent request. The developers used some workaround or@SessionAttributesto store the values. With the release of Spring 3.1, flash attributes solve that issue. Spring container stores the variables temporarily and destroys them after the request.

Are looking for the Spring beginner tutorials? Read our Getting Started Spring Tutorials .


package javabeat.net;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

public class FlashAttributesController {

  public String getResult(@ModelAttribute("user") UserDetails userDetails,
       final RedirectAttributes redirectAttributes){
    //Some logics goes here.

    //Before redirect set some values to Flash attributes
    redirectAttributes.addFlashAttribute("user", userDetails);
    redirectAttributes.addFlashAttribute("message", "Welcome to test page!!!");

    return "redirect:/flashredirect";

  public String getResultRedirect(@ModelAttribute UserDetails userDetails ){
    return "example";

  public UserDetails getUser(@RequestParam String userName){
    UserDetails details = new UserDetails();
    return details;


  <h1>${message} for user ${user.userName}</h1>
  1. Add the RedirectAttributes in the controller method arguments.
  2. redirectAttributes.addFlashAttribute for adding any number of variables in the scope. Note that, this values are persisted for the very short time. Till the next request. we are not in the controll of where the variables are stored.
  3. While returning the view, use “redirect” for making the new requtest which is also defined in the same controller class.
  4. I have define @ModelAttribure for storing the values to a model object. The values are coming from the request parameter. This method is always invoked bfore the controller method.
  5. The above jsp file simply prints the message which is stored in the flash attributes.
  6. The above example is only for demonstrating the power of flash attributes, in real time the above example may not make sense because I have used model attributes to store the values, etc. only for making this example to run.

The spring configuration file is very much similar to any other examples used for the spring mvc application . However, don’t forget to add the following line of snippet in the configuration file.

<mvc:annotation-driven />

If you miss the above line, you may see the error like one below.

nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException:
Failed to invoke handler method [public java.lang.String javabeat.net.FlashAttributesController.
RedirectAttributes)]; nested exception is java.lang.IllegalStateException: Argument [RedirectAttributes] is of
type Model or Map but is not assignable from the actual model. You may need to switch newer MVC infrastructure
classes to use this argument.

If you run the application and access the web page using this URL:


It will be reditec to this page:



Are looking for the Spring beginner tutorials? Read our Getting Started Spring Tutorials .


在Spring MVC 中使用 flash attributes

Spring MVC 学习(六) Using flash attributes By yulezhandian 2013 年 1 月 14 日 Spring MVC No Comments 17...
  • z69183787
  • z69183787
  • 2014年12月03日 09:53
  • 1643

spring3.1.1 mvc使用注解搭建hello world

使用spring3.1.1中的mvc搭建项目,写一个hello world出来。 看了两天官方文档,不管好坏,就是组织的比较零散,不能有序的给人一个项目基本的轮廓。而且里面的例子也不是最佳实践。 ...
  • mzbzyc
  • mzbzyc
  • 2014年06月06日 15:36
  • 939

Spring MVC Flash Attribute 的讲解与使用示例

Spring MVC 3.1版本加了一个很有用的特性,Flash属性,它能解决一个长久以来缺少解决的问题,一个POST/Redirect/GET模式问题。 正常的MVC Web应用程序在每次提交...
  • u012843873
  • u012843873
  • 2016年07月26日 11:05
  • 897

Spring 3.1MVC 实战

Spring 3.1MVC 实战   一、spring3.1 mvc核心思想介绍... 2 二、spring3.1 mvc 框架的特点... 3 三、spring 3.1 MVC入门列子H...
  • sust2012
  • sust2012
  • 2013年01月05日 12:38
  • 3652

10、Spring MVC 之 Flash attributes

Flash attributes提供一种方式给一个request储存attributes用来给另一个请求使用。这是最常见的重定向时所需要的–例如,Post/Redirect/Get模式。在重定向之前F...
  • u012410733
  • u012410733
  • 2016年09月28日 22:20
  • 440

Spring配置事务中的 transactionAttributes 各属性含义及XML配置

transactionAttributes 属性: PROPAGATION 事务传播行为类型 说明 PROPAGATION_REQUIRED 如果当前没有事务,就...
  • z69183787
  • z69183787
  • 2013年12月06日 09:50
  • 11256


  • qq_18675693
  • qq_18675693
  • 2016年08月25日 20:17
  • 417


简单的来说转发就是浏览器不刷新,重定向会告诉浏览器刷新了. 复杂来说的转发就是服务区内容器存储的问题,二转发就不会带着原来的参数去新的地址了. 来看下springMVC转发和重定向的要点: 1...
  • kidoo1012
  • kidoo1012
  • 2017年01月12日 21:04
  • 228


  • FansUnion
  • FansUnion
  • 2015年02月07日 20:40
  • 1574

零基础搭建 spring mvc 4 项目(本文基于 Servlet 3.0)

作者各必备工具的版本如下:Tomcat:apache-tomcat-7.0.63 (下载链接)Java EE - Eclipse:Luna Service Release 1 v4.4.1 (下载链接...
  • defonds
  • defonds
  • 2015年08月27日 18:38
  • 12213
您举报文章:Flash Attributes in Spring MVC 3.1