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 .

FlashAttributesController.java

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;

@Controller
public class FlashAttributesController {

  @RequestMapping(value="/flashexample")
  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";
  }

  @RequestMapping(value="/flashredirect")
  public String getResultRedirect(@ModelAttribute UserDetails userDetails ){
    return "example";
  }

  @ModelAttribute("user")
  public UserDetails getUser(@RequestParam String userName){
    UserDetails details = new UserDetails();
    details.setUserName(userName);
    return details;
  }
}

example.jsp

<html>
<body>
  <h1>${message} for user ${user.userName}</h1>
</body>
</html>
  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.
getResult(javabeat.net.UserDetails,org.springframework.web.servlet.mvc.support.
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:

http://localhost:8080/SpringExamples/flashexample?userName=TestUser

It will be reditec to this page:

http://localhost:8080/SpringExamples/flashredirect

Flash-Attributes-Spring-Mvc



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

FlashAttributesController.java

SpringMVC整合Mongodb开发 架构搭建

SpringMVC整合Mongodb开发 架构搭建 转载自:[http://www.cnblogs.com/dennisit/p/3372568.html] 系统环境: 操作...
  • wgrzhuaq
  • wgrzhuaq
  • 2016年07月07日 11:00
  • 1109

Spring4 Spring MVC实战(一)——读《Spring in action》搭建最简单的MVC

现在还在用Struts吗,是的,不说你在用Spring MVC,我都觉得不好意思了。前面的时候所谓的MVC模式中,C的角色交由Struts控制。而Spring MVC这样叫了,字眼上大概知道这东西是干...
  • iaiti
  • iaiti
  • 2016年09月22日 09:30
  • 3100

spring-mvc.xml文件的配置(官方文件)

spring-mvc.xml文件配置
  • high2011
  • high2011
  • 2015年09月30日 16:03
  • 5594

10、Spring MVC 之 Flash attributes

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

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...
  • gmemai
  • gmemai
  • 2015年09月09日 07:14
  • 443

Spring MVC 3.1新特性 生产者、消费者请求限定

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

myeclipse2013+spring mvc 3.1+hibernate 4.1.4集成

最近正在学习springmvc和hibernate的集成框架,经过一个多星期的查询资料终于实现了hibernate4 和hibernate3有一些区别,hibernate4抛弃了hibernate3的...
  • zql781744363
  • zql781744363
  • 2013年11月27日 15:09
  • 1793

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

在使用Mybatis3.10+spring3.10+mybatis-spring-1.0.0集成,使用spring 时发生如下错误: 严重: Servlet.service() ...
  • u013948187
  • u013948187
  • 2015年07月11日 07:18
  • 478

spring MVC 3.1 forword/redirect

spring mvc 如何请求转发和重定向呢?往下看: 由于这部分内容简单,一带而过了。   1.请求转发:    (1)返回ModelAndView : @Reques...
  • a137268431
  • a137268431
  • 2013年12月17日 18:52
  • 6641

spring mvc+jquery uploadify3.1上传

最近看到一个用jquery uploadify上传的例子,然后自己把它和spring mvc整合了一下,废话不多说,图码一起上 自己去下载jquery uploadify这个插件,后台需要引入comm...
  • yangxuan0261
  • yangxuan0261
  • 2013年08月04日 13:32
  • 4455
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Flash Attributes in Spring MVC 3.1
举报原因:
原因补充:

(最多只允许输入30个字)