spring mvc redirect 重定向 跳转并传递参数
来源:互联网 发布:四六级听力软件 编辑:程序博客网 时间:2024/05/29 18:31
前言
在项目中做form表单功能提交时,防止用户客户端后退或者刷新时重复提交问题,需要在服务端进行重定向跳转,具体跳转方式有以下几种方式
公用代码:
@RequestMapping(value="/index",method = { RequestMethod.POST, RequestMethod.GET }) public ModelAndView index(HttpServletResponse response){ ModelAndView model = new ModelAndView("/home/index"); return model; }
一、使用HttpServletResponse 进行重定向跳转
@RequestMapping(value="/toIndex",method = { RequestMethod.POST, RequestMethod.GET }) public ModelAndView toIndex(HttpServletResponse response){ try { response.sendRedirect("/index"); } catch (IOException e1) { } return null;
二、依赖spring mvc的 ViewResolver直接跳转
@RequestMapping(value="/toIndex",method = { RequestMethod.POST, RequestMethod.GET }) public String toIndex(HttpServletResponse response){ return "redirect:/index"; }
注:当需要传递简单参数时可以使用以上两种方式通过get方式将参数拼接到url路劲后面。
三、依赖Spring mvc的RedirectAttributes
@RequestMapping(value="/toIndex",method = { RequestMethod.POST, RequestMethod.GET }) public String toIndex(HttpServletResponse response,RedirectAttributes model){ model.addFlashAttribute("userName", 'TimerBin'); model.addFlashAttribute("userPass", 'ApeVm23U3wxEGocX'); return "redirect:/index"; }
在/home/index 可以直接使用
在controller中获取放在RedirectAttributes中的userName信息的方式:
@RequestMapping(value="/index",method = { RequestMethod.POST, RequestMethod.GET }) public ModelAndView index(@ModelAttribute("userName") String userName){ ModelAndView model = new ModelAndView("/main/index"); model.addObject("userName", userName); return model; }
注:在项目中使用RedirectAttributes,因为该对象就是把参数信息放到项目中的session中,再多台服务器中使用该对象存储参数时已经要保证sesion设置是粘性的,不然在集群服务器中不支持该对象的使用!
阅读全文
0 0
- spring mvc redirect 重定向 跳转并传递参数
- spring mvc redirect 重定向 跳转并传递参数
- spring mvc redirect 重定向 跳转并传递参数
- spring mvc redirect 重定向 跳转并传递参数 && mvc:view-controller直接转发页面
- spring mvc 重定向时参数传递
- Spring mvc重定向传递参数
- Spring MVC重定向参数传递
- springMVC重定向redirect传递参数
- spring mvc跳转重定向
- 【spring mvc】 页面重定向时参数的传递
- Spring mvc redirect 传递参数乱码
- spring mvc redirect 传递参数 和接收
- Spring MVC 跳转(2)-重定向
- Spring MVC 带参数重定向
- Spring MVC重定向传参数
- Spring mvc redirect传递参数时异常RedirectAttributes
- springMVC controller间跳转 重定向 传递参数的方法
- springMVC controller间跳转 重定向 传递参数的方法
- Codeforces Round #420 (Div. 2) D
- 线程死锁
- 时间的获取与格式转换
- OpenCV学习笔记(27) 灰度直方图均衡化 练习
- log4j的8个日志级别(OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、 ALL)
- spring mvc redirect 重定向 跳转并传递参数
- 进程注入后门工具Cymothoa
- TiDB RC3 Release
- iOS
- poj2155 Matrix 二维树状数组
- 在命令行中使用Eclipse MAT工具
- 干货来袭,推荐几款开源的Kotlin的Android项目
- leetcode 628:Maximum Product of Three Numbers
- 计算两个NSDate的时间差,计算毫秒差