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 .



10、Spring MVC 之 Flash attributes

Flash attributes提供一种方式给一个request储存attributes用来给另一个请求使用。这是最常见的重定向时所需要的–例如,Post/Redirect/Get模式。在重定向之前F...

How to use Session attributes in Spring-mvc

Could you help me write spring mvc style analog so code in jsp and servlets? session.setAttribute("n...

spring mvc(3.1)包

  • 2013-10-22 13:19
  • 2.51MB
  • 下载

Spring MVC3.1中文文档

  • 2016-09-20 07:13
  • 2.77MB
  • 下载

myeclipse2013+spring mvc 3.1+hibernate 4.1.4集成

最近正在学习springmvc和hibernate的集成框架,经过一个多星期的查询资料终于实现了hibernate4 和hibernate3有一些区别,hibernate4抛弃了hibernate3的...

spring mvc +Mybatis3.1 整合的时候异常

在使用Mybatis3.10+spring3.10+mybatis-spring-1.0.0集成,使用spring 时发生如下错误: 严重: Servlet.service() ...

spring MVC 3.1 forword/redirect

spring mvc 如何请求转发和重定向呢?往下看: 由于这部分内容简单,一带而过了。   1.请求转发:    (1)返回ModelAndView : @Reques...

spring mvc+jquery uploadify3.1上传

最近看到一个用jquery uploadify上传的例子,然后自己把它和spring mvc整合了一下,废话不多说,图码一起上 自己去下载jquery uploadify这个插件,后台需要引入comm...

Spring Mvc 3.1 之后如何配置messageConverters

是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethod...

Spring MVC 3.1新特性 生产者、消费者请求限定 ——注解式控制器详解

6.6.5、生产者、消费者限定、基本概念 首先让我们看一下通过HTTP协议传输的媒体类型及如何表示媒体类型:   一、Media Type: 互联网媒体类型,一般就是我们所说...