Flash Attributes in Spring MVC 3.1
2015-07-09 11:51
603 查看
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
example.jsp
Add the RedirectAttributes in the controller method arguments.
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.
While returning the view, use “redirect” for making the new requtest which is also defined in the same controller class.
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.
The above jsp file simply prints the message which is stored in the flash attributes.
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.
If you miss the above line, you may see the error like one below.
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 .
FlashAttributesController.java
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>
Add the RedirectAttributes in the controller method arguments.
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.
While returning the view, use “redirect” for making the new requtest which is also defined in the same controller class.
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.
The above jsp file simply prints the message which is stored in the flash attributes.
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
Are looking for the Spring beginner tutorials? Read our Getting Started Spring Tutorials .
FlashAttributesController.java
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Zend的MVC机制使用分析(二)
- ASP.NET MVC 4 捆绑和缩小实例介绍
- ASP.NET MVC中将控制器分离到类库的实现
- asp.net实现在非MVC中使用Razor模板引擎的方法
- ASP.NET MVC中的AJAX应用
- 如何在MVC应用程序中使用Jquery
- ASP.NET MVC小结之基础篇(二)
- ASP.NET小结之MVC, MVP, MVVM比较以及区别(一)
- Asp.net实现MVC处理文件的上传下载功能实例教程
- ASP.NET MVC小结之基础篇(一)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- Flex中最好的MVC框架Mate框架
- struts2 spring整合fieldError问题
- ASP.NET MVC学习笔记
- 关于php mvc开发模式的感想