Spring3中的FlashAttribute

为 了防止用户刷新重复提交,save操作之后一般会redirect到另一个页面,同时带点操作成功的提示信息。因为是Redirect,Request里 的attribute不会传递过去,如果放在session中,则需要在显示后及时清理,不然下面每一页都带着这个信息也不对。Spring在3.1才提 供了这个能力。

记得在spring mvc2中,当保存POJO到数据库后,要返回成功页面,如果这个时候要带点信息, 则要这样:

    //第三个参数(UserModel user)默认为绑定对象
@RequestMapping(value = "/user/save", method = RequestMethod.POST)
public ModelAndView saveUser(HttpServletRequest request, HttpServletResponse response,UserModel user) throws Exception {
ModelAndView mv = new ModelAndView("/user/save/result");//默认为forward模式
// ModelAndView mv = new ModelAndView("redirect:/user/save/result");//redirect模式
mv.addObject("message","保存用户成功!");
return mv;
}

而在spring mvc 3.1后,可以这样

    @RequestMapping(value = "/user/save", method = RequestMethod.POST)
public ModelAndView saveUser(UserModel user, RedirectAttributes redirectAttributes) throws Exception {
redirectAttributes.addFlashAttribute("message", "保存用户成功!");//使用addFlashAttribute,参数不会出现在url地址栏中
return "redirect:/user/save/result";
}

来个稍微完整点的例子,首先是一个表单,在其中填入一些信息:

<form:form id="myform" action="saveUserDetails.action" method="POST" commandName="user">
<form:input type="text" name="firstName" path="firstName"/>
<form:input type="text" name="lastName" path="lastName"/>
<form:input type="text" name="email" path="email"/>
<input type="submit" value="submit">
</form:form>

则在controller中,可以这样:

 @RequestMapping(value="/saveUserDetails.action", method=RequestMethod.POST)
public String greetingsAction(@Validated User user,RedirectAttributes redirectAttributes){
someUserdetailsService.save(user);
redirectAttributes.addFlashAttribute("firstName", user.getFirstName());
redirectAttributes.addFlashAttribute("lastName", user.getLastName())
return "redirect:success.html";
}

success.html:

<div>
<h1>Hello ${firstName} ${lastName}. Your details stored in our database.</h1>
</div><br>

但如果F5的时候,会发现参数丢失,因为flash scope其实只支持redirect的,所以可以判断下:

@RequestMapping(value="/success.html", method=RequestMethod.GET)
public String successView(HttpServletRequest request){
Map<String,?> map = RequestContextUtils.getInputFlashMap(request);
if (map!=null)
return "success";
else return "redirect:someOtherView"; //給出其他提示信息

Spring中RedirectAttributes对象重定向传参的更多相关文章

  1. spring mvc controller间跳转 重定向 传参(转)

    spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...

  2. SpringMVC归纳-1(model数据模型与重定向传参技术)

    要点: model是一个Map结构的数据模型,能重定向时传递数据(拼接URL),但不安全,主要用于渲染前端页面,配合Thymeleaf填充html里面里设置好的参数. @RequestParam用来获 ...

  3. jsp内部传参与重定向传参

    1 重定向地址栏会发生改变,因为它会发送两次请求,内部转发,地址栏不会发生改变,因为它只有一个请求2 重定向不能获取上一次请求中的参数,而内部转换可以3 内部转发可以访问WEB-INF下的资源,重定向 ...

  4. js中使用进行字符串传参

    在js中拼接html标签传参时,如果方法参数是字符串需要加上引号,这里需要进行字符转义 <a href='javascript:addMenuUI("+"\"&qu ...

  5. Ajax之Json对象序列化传参

    前端部分:传入参数 "groupObject" : jsonString //对象序列化传参 var projectGroup = {}; projectGroup["i ...

  6. SpringMVC中使用RedirectAttributes重定向传参,防止暴露参数

    RedirectAttributes是SpringMVC3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的. 当我从jsp页面函数中带参数到controller层方法,方法执行完毕后返回 ...

  7. Spring中RedirectAttributes的用法

    RedirectAttributes 是Spring mvc 3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的的工具类.他有两种带参的方式: 第一种: redirectAttribut ...

  8. requests中get和post传参

    get请求 get(url, params=None, **kwargs) requests实现get请求传参的两种方式 方式一: import requests url = 'http://www. ...

  9. 谈谈Spring中的对象跟Bean,你知道Spring怎么创建对象的吗?

    本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 推荐阅读: Spring官网阅读 | 总结篇 Spring杂 ...

随机推荐

  1. Python修饰器的函数式编程

    Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...

  2. mysql取整,小数点处理函数floor(), round()

    mysql数值处理函数floor与round    在mysql中,当处理数值时,会用到数值处理函数,如有一个float型数值2.13,你想只要整数2,那就需要下面的函数floor与round.   ...

  3. Linq To SQLite by CRUD

    1, 希望使用linqtoSQLite 来对数据库实现CRUD, 开发环境 VS2013, 1.1 在网上找到了 LINQ to DB T4 Models, 配置参考网址链接: http://www. ...

  4. java数据结构和算法------冒泡排序

    package iYou.neugle.sort; public class Bubble_sort { public static void BubbleSort(double[] array) { ...

  5. 新手学Android

    Eclipse平台下的新手Android学习记录. 1.打开一个本地的项目 在Project Explorer右键->Import->Existing Projects into Work ...

  6. action属性

    action属性 2013年7月8日 14:52 Path: action的访问路径,以"/"开头 Type: action的类型 Name: action使用的actionFor ...

  7. 转载:SQL索引一步到位

    原文: http://www.cnblogs.com/AK2012/archive/2013/01/04/2844283.html SQL索引一步到位(此文章为“数据库性能优化二:数据库表优化”附属文 ...

  8. Android下写一个永远不会被KILL掉的进程/服务

    Android 系统对于内存管理有自己的一套方法,为了保障系统有序稳定的运信,系统内部会自动分配,控制程序的内存使用.当系统觉得当前的资源非常有限的时候,为了保证一些优先级高的程序能运行,就会杀掉一些 ...

  9. mysql 存储过程 -- 游标的使用(备忘)

    BEGIN ; DECLARE f_ratio FLOAT DEFAULT 0.8; ); ); DECLARE i_statDate DATE; DECLARE i_accumulateCount ...

  10. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...