Spring中RedirectAttributes对象重定向传参
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对象重定向传参的更多相关文章
- spring mvc controller间跳转 重定向 传参(转)
spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...
- SpringMVC归纳-1(model数据模型与重定向传参技术)
要点: model是一个Map结构的数据模型,能重定向时传递数据(拼接URL),但不安全,主要用于渲染前端页面,配合Thymeleaf填充html里面里设置好的参数. @RequestParam用来获 ...
- jsp内部传参与重定向传参
1 重定向地址栏会发生改变,因为它会发送两次请求,内部转发,地址栏不会发生改变,因为它只有一个请求2 重定向不能获取上一次请求中的参数,而内部转换可以3 内部转发可以访问WEB-INF下的资源,重定向 ...
- js中使用进行字符串传参
在js中拼接html标签传参时,如果方法参数是字符串需要加上引号,这里需要进行字符转义 <a href='javascript:addMenuUI("+"\"&qu ...
- Ajax之Json对象序列化传参
前端部分:传入参数 "groupObject" : jsonString //对象序列化传参 var projectGroup = {}; projectGroup["i ...
- SpringMVC中使用RedirectAttributes重定向传参,防止暴露参数
RedirectAttributes是SpringMVC3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的. 当我从jsp页面函数中带参数到controller层方法,方法执行完毕后返回 ...
- Spring中RedirectAttributes的用法
RedirectAttributes 是Spring mvc 3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的的工具类.他有两种带参的方式: 第一种: redirectAttribut ...
- requests中get和post传参
get请求 get(url, params=None, **kwargs) requests实现get请求传参的两种方式 方式一: import requests url = 'http://www. ...
- 谈谈Spring中的对象跟Bean,你知道Spring怎么创建对象的吗?
本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 推荐阅读: Spring官网阅读 | 总结篇 Spring杂 ...
随机推荐
- 001--VS2013 c++ 游戏框架
头文件:MainClass.h 内容: #include <Windows.h> //全局函数声明LRESULT CALLBACK WndProc(HWND hwnd, UINT mess ...
- LinuxC 文件与目录 打印文件操作错误信息
打印文件操作错误信息 在进行文件操作是,会遇到权限不足.找不到文件等错误,可以在程序中设置错误捕捉语句并显示错误.错误捕捉和错误输出使用用错误号和streero实现. 函数原型 : char *str ...
- 【转】MATLAB在一幅图上添加多个纵坐标轴
来源:http://wenku.baidu.com/link?url=m_eEkzbjnT9ccgAnlPVDqHCVyrZOD2EplXxxIiQc69DI0lHAWzwpZXfdDy_7DPbwI ...
- 【AFNetworking】AFNetworking源码阅读(一)
1. 前言 2. iOS Example代码结构 3.AFNetworkActivityIndicatorManager 4. UIRefreshControl+AFNetworking 5. AFN ...
- How to compile and install NCAR Command Language on IBM PowerPC 64 --- NCL编译安装步骤
作者:Sinsonglew 出处:http://www.cnblogs.com/sinsonglew 欢迎转载,也请保留这段声明.thanks :) 注记:NCL官方依赖安装包全集列表.官方源码编译指 ...
- C#制作高仿360安全卫士窗体(三)
距上篇C#制作高仿360安全卫士窗体(二)也将近一个多月了,这个月事情还是像往常一样的多.不多我也乐在其中,毕竟我做的是我喜欢做的东西.今天特地抽空把怎么制作文本框写一下.同时也希望有爱好这些玩意的同 ...
- 6、android开发中遇到的bug整理
1.使用actionProvider时出现的问题 bug复现: 解决方案: //import android.support.v4.view.ActionProvider; import androi ...
- linux下在某行的前一行或后一行添加内容
linux的sed工具是十分强大的,能很容易的实现在某关键词的前一行或后一行增加内容.今天在批量修改tomcat的日志时就用到了该功能. 一.在某行的前一行或后一行添加内容 具休操作如下: #匹配行前 ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- 【python】文件的输入和输出
1.os模块 2.os.path 模块 3.实例 1. os模块 对文件系统的访问大多通过python的os模块实现,其中os 模块负责大部分的文件系统操作,包括删除/重命名文件,遍历目录树,管理文件 ...