在项目中做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. 【MongoDB】在windows平台下mongodb的分片集群(六)

    在本篇博客中我们主要讨论下博客的管理.因为已经在前面五篇中写了具体的实例,因此这里就不再举例说明. 一.监控 分片集群是整个体系中比較复杂的一块,因此更应该须要监控. 主要命令: serverstat ...

  2. 九度OJ—题目1032:ZOJ

    题目描写叙述: 读入一个字符串.字符串中包括ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出.当某个字符用完时,剩下的仍然依照ZOJ的顺序输出. 输入: 题目包括多组用例,每组用例占一行,包括ZOJ ...

  3. golang 获取环境信息

    os.Environ() os.Getenv("TMP")

  4. Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明

    http://blog.csdn.net/centre10/article/details/6050466 来自于:http://blog.csdn.net/centre10/article/deta ...

  5. 强大的xUtils工具类整理

    xUtils简单介绍 xUtils 包括了非常多有用的android工具. xUtils 支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的ORM,很多其它的事件注解支持且不受 ...

  6. Project Euler 516 5-smooth totients (数论)

    题目链接: https://projecteuler.net/problem=516 题目: \(5\)-smooth numbers are numbers whose largest prime ...

  7. Let's do our own full blown HTTP server with Netty--转载

    原文地址:http://adolgarev.blogspot.com/2013/12/lets-do-our-own-full-blown-http-server.html Sometimes ser ...

  8. MongoDbHelper 帮助类(下)

    对MongoDbHelper帮助类进行了一下整合,但是代码中一个方法需要将string类型转化为BsonValue类型一直出错.所以欢迎留言指正 using System; using System. ...

  9. UVA 11178 - Morley's Theorem 向量

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. 总览:SpringCloud基础结构

    客户端: 1.连接 配置中心2.连接 服务中心3.连接 链路跟踪3.feign调用4.熔断机制5.连接 熔断监控6.swagger2 生成的RESTful-API7.消息总线-rabbitMQ实现 基 ...