在项目中做form表单功能提交时,防止用户客户端后退或者刷新时重复提交问题,需要在服务端进行重定向跳转,具体跳转方式有以下几种方式:

公用代码:

  1. @RequestMapping(value=“/index”,method = { RequestMethod.POST, RequestMethod.GET })
  2. public  ModelAndView index(HttpServletResponse response){
  3. ModelAndView model = new ModelAndView(“/home/index”);
  4. return model;
  5. }
@RequestMapping(value="/index",method = { RequestMethod.POST, RequestMethod.GET })
public ModelAndView index(HttpServletResponse response){
ModelAndView model = new ModelAndView("/home/index");
return model;
}

一、使用HttpServletResponse 进行重定向跳转

  1. @RequestMapping(value=“/toIndex”,method = { RequestMethod.POST, RequestMethod.GET })
  2. ublic  ModelAndView toIndex(HttpServletResponse response){
  3. try {
  4. response.sendRedirect(”/index”);
  5. } catch (IOException e1) {
  6. }
  7. return null;
       @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直接跳转

  1. @RequestMapping(value=“/toIndex”,method = { RequestMethod.POST, RequestMethod.GET })
  2. public  String toIndex(HttpServletResponse response){
  3. return “redirect:/index”;
  4. }
@RequestMapping(value="/toIndex",method = { RequestMethod.POST, RequestMethod.GET })
public String toIndex(HttpServletResponse response){
return "redirect:/index";
}

注:当需要传递简单参数时可以使用以上两种方式通过get方式将参数拼接到url路劲后面。

三、依赖Spring mvc的RedirectAttributes 

  1. @RequestMapping(value=“/toIndex”,method = { RequestMethod.POST, RequestMethod.GET })
  2. public  String toIndex(HttpServletResponse response,RedirectAttributes model){
  3. model.addFlashAttribute(”userName”, ‘TimerBin’);
  4. model.addFlashAttribute(”userPass”, ‘ApeVm23U3wxEGocX’);
  5. return “redirect:/index”;
  6. }
@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 可以直接使用{&lt;/span&gt;&lt;span style=&quot;font-family: monospace; line-height: 1.5; background-color: #fafafa;&quot;&gt;userName&lt;/span&gt;&lt;span style=&quot;font-family: monospace; line-height: 1.5; background-color: #fafafa;&quot;&gt;},&lt;/span&gt;&lt;span style=&quot;line-height: 1.5; font-family: monospace; background-color: #fafafa;&quot;&gt;" role="presentation" style="position: relative;">{</span><span style="font-family: monospace; line-height: 1.5; background-color: #fafafa;">userName</span><span style="font-family: monospace; line-height: 1.5; background-color: #fafafa;">},</span><span style="line-height: 1.5; font-family: monospace; background-color: #fafafa;">{</span><span style="font-family: monospace; line-height: 1.5; background-color: #fafafa;">userName</span><span style="font-family: monospace; line-height: 1.5; background-color: #fafafa;">},</span><span style="line-height: 1.5; font-family: monospace; background-color: #fafafa;">{userPass}来获取重定向跳转的参数信息,这种方式可以处理复杂的参数传值问题,还可以使用此种方式来隐藏或缩短原有请求URL信息。

在controller中获取放在RedirectAttributes中的userName信息的方式:

  1. @RequestMapping(value=“/index”,method = { RequestMethod.POST, RequestMethod.GET })
  2. public  ModelAndView index(@ModelAttribute(“userName”) String userName){
  3. ModelAndView  model = new ModelAndView(“/main/index”);
  4. model.addObject(”userName”, userName);
  5. return model;
  6. }
@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设置是粘性的,不然在集群服务器中不支持该对象的使用!

spring mvc redirect 重定向 跳转并传递参数的更多相关文章

  1. Spring MVC(十)--通过表单序列化传递参数

    通过表单序列化传递参数就是将表单数据转化成字符串传递到后台,序列化之后参数请求变成这种模式param1=value1&&param2=value2,下面用代码实现. 1.创建表单 &l ...

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

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

  3. Spring MVC页面重定向

    以下示例显示如何编写一个简单的基于Web的重定向应用程序,这个应用程序使用重定向将http请求传输到另一个页面. 基于Spring MVC - Hello World实例章节中代码,创建创建一个名称为 ...

  4. mvc中view与controll之间传递参数时,可以使用url进行传递

    mvc中view与controller之间传递参数时,可以使用url进行传递,但是在url的地址中需要加上“id=123”这样的东西才行. 具体如代码: window.location.href = ...

  5. [Xcode 实际操作]九、实用进阶-(24)使用Segue(页面的跳转连接)进行页面跳转并传递参数

    目录:[Swift]Xcode实际操作 本文将演示使用Segue(页面的跳转连接)进行页面跳转并传递参数. 参照结合:[Xcode10 实际操作]九.实用进阶-(23)多个Storyboard故事板中 ...

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

    转自:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景     需求:spring MVC框架contr ...

  7. Spring Mvc Controller间跳转 重定向 传参 (转)

    原文链接:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景     需求:spring MVC框架con ...

  8. spring mvc controller间跳转 重定向

    1. 需求背景     需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...

  9. Spring MVC 页面跳转时传递参数

    页面仍然使用 JSP,在跳转时如果想传递参数则需要用到类 RedirectAttributes. 首先看看如何打开一个普通页面: // 登录页面(每个页面都要独立的 Action 来支持其呈现) @R ...

随机推荐

  1. 洛谷——P1540 机器翻译

    https://www.luogu.org/problem/show?pid=1540#sub 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的 ...

  2. H5移动端IOS/Android兼容性总结,持续更新中…

    H5移动端IOS/Android兼容性总结,持续更新中… 1. IOS不识别日期 new Date("2018-07-01 08:00:00")在Android下正常显示可以直接进 ...

  3. $OEM$文件夹的使用 (By无约而来)

    WIN7-OEM资料包中的目录都是以$OEM$文件夹出现的.比$OEM$高一级的目录,我通常是用来表示下一级的$OEM$的属性,例如,X64_ADMIN_LOADER表示此目录下的$OEM$文件夹是用 ...

  4. <一> 爬虫的原理

    一.爬虫是什么? #1.什么是互联网? 互联网是由网络设备(网线,路由器,交换机,防火墙等等)和一台台计算机连接而成,像一张网一样. #2.互联网建立的目的? 互联网的核心价值在于数据的共享/传递:数 ...

  5. postman--下载及使用入门

    安装 本文只是基于 Chrome 浏览器的扩展插件来进行的安装,并非单独应用程序. 首先,你要台电脑,其次,安装有 Chrome 浏览器,那你接着往下看吧. 1. 官网安装(别看) 打开官网,http ...

  6. 火狐与IE的7个JavaScript差异

    作者注:本篇文章发表于2009.04.27,是一篇关于讨论Javascript在IE6.IE7和FF2+.FF3.0之间的存在的问题的文章. 虽然须要用冗长的JavaScript代码去识别特定的浏览器 ...

  7. 关于LWIP断开网线后重连问题(热插拔问题)

    近期在弄STM32+LWIP协议.在网络拔掉网线情况下.无法又一次连接. 网上找了好多方法都没有实现,着实郁闷! 后来无意间看到了临时解决这一问题的方法.尽管不是那么完美,但最算能解决这个问题.分享给 ...

  8. js用button激活 Alert 元素关闭按钮的交互功能

    js用button激活 Alert 元素关闭按钮的交互功能 一.总结 1.点(.)对应class,井号(#)对应id  2.jquery:amaze里面用的jquery,jquery熟悉之后,这些东西 ...

  9. C语言深度剖析-----函数与指针的分析

                          指针的本质 指针需要保证指向任意数据类型,所以指针变量都占用32位bit即4字节. PS:不同机器上,指针占用内存不一                   ...

  10. LA 3882 - And Then There Was One(约瑟夫 递归)

    看题传送门 题目大意: N个数排成一圈,第一次删除m,以后每k个数删除一次,求最后一被删除的数. 如果这题用链表或者数组模拟整个过程的话,时间复杂度都将高达O(nk),而n<=10000,k&l ...