在项目中做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. 洛谷 P2819 图的m着色问题

    P2819 图的m着色问题 题目背景 给定无向连通图G和m种不同的颜色.用这些颜色为图G的各顶点着色,每个顶点着一种颜色.如果有一种着色法使G中每条边的2个顶点着不同颜色,则称这个图是m可着色的.图的 ...

  2. 【Python】用Python的“结巴”模块进行分词

    之前都是用计算所的分词工具进行分词,效果不错可是比較麻烦,近期開始用Python的"结巴"模块进行分词,感觉很方便.这里将我写的一些小程序分享给大家,希望对大家有所帮助. 以下这个 ...

  3. 囧 appspot.com/

    囧 appspot.com/ 我负责公司人事,最近车间招了一批外来打工妹,让她们填写个人资料表格,早上在看表格登记,发现其中一张政治面貌一栏赫然写着"瓜子脸",当时笑得眼泪直流,没 ...

  4. CentOS6.X安装10G需要额外安装的软件包

    yum -y install libXp yum -y install libXp.i686 yum -y install libXtst.i686

  5. axios封装http请求

    import axios from 'axios' const HTTP_TIMEOUT = 15000; export function httpPost(url, params = {},head ...

  6. 5. Node基础编程

      基于Chrome V8引擎 单线程 使用JavaScript开发后端代码 非阻塞的IO common规范 common一定是通过module.exports={}输出 创建Http Server ...

  7. hdu5389

    题意:给你n个人每一个人手里有一个id,然后给你两个数a和b.让你把n个人分为两组.条件是 一组人手里的id和等于a 另一组人的id和等于b,这里的和是指加起来之后对9取余,假设sum等于0 则sum ...

  8. Java核心技术 卷Ⅰ 基础知识(7)

    第13章 集合 集合接口 具体的集合 在表中,除了Map结尾的类之外,其他类都实现了Collection接口,而以Map结尾的类实现了Map接口. 链表 数组列表 散列集 树集 双端队列 优先级队列 ...

  9. JS实现动画的四条优化方法

    JS实现动画的四条优化方法 1)如果使用的是setTimeout实现的轮询动画,在每一次执行方法之前需要把前面的设置的定时器清除掉 2)为了防止全局变量的污染,我们把定时器的返回值赋值给当前操作元素的 ...

  10. (转)iptables详细教程:基础、架构、清空规则、追加规则、应用实例

    转自:http://lesca.me/archives/iptables-tutorial-structures-configuratios-examples.html iptables防火墙可以用于 ...