spring mvc redirect 重定向 跳转并传递参数

news/2024/7/5 6:05:50

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

公用代码:

Java代码   收藏代码spinner.gif
  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 进行重定向跳转

  

Java代码   收藏代码spinner.gif
  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直接跳转

 

Java代码   收藏代码spinner.gif
  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 

 

Java代码   收藏代码spinner.gif
  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 可以直接使用{</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信息的方式:

 

Java代码   收藏代码spinner.gif
  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设置是粘性的,不然在集群服务器中不支持该对象的使用!

 

 

 

 


http://www.niftyadmin.cn/n/3367205.html

相关文章

游戏行业术语一览(2)--游戏运营转化率[转载]

转化率这个指标在各行各业的数据分析中运用的非常之广泛&#xff0c;例如&#xff1a;电商中就会存在&#xff0c;点击到订单生成的一系列转化率&#xff0c;传统的销售行业也会在做广告的时候考虑该广告能够转化多少订单&#xff0c;而在游戏行业&#xff0c;转化率同样是一个…

记mysql中时间相关的一个奇怪问题

发现mysql中类型为时间的字段&#xff0c;在查询时显示的时间是什么是依赖于客户端的&#xff0c;不同的客户端查同一个时间&#xff0c;可能在客户端显示的时间是不一样的。至于这个在哪里配置&#xff0c;以及服务端如何依据这个配置为客户端返回结果&#xff0c;这个还没深入…

如何整合JIRA、FishEye、Crucible进行CodeReview

Atlassian的东西相信大家都不陌生&#xff0c;知道Confluence的有木有&#xff0c;用过JIRA的有木有&#xff0c;虽然说这些产品都要收费&#xff0c;不过对于学校和开源组织都可以申请免费License&#xff0c;也可以申请试用。 前段时间为了方便做CodeReview&#xff0c;尝试了…

列表,循环

二维列表 list1 [[1,2,3], [1,2,3], [1,2,3],] print(lsit[1][1]) 列表方法 1、append()  在列表末尾添加新的元素 2、extend()  在末尾一次性追加另一个列表中的多个值 3、insert()  下标处添加一个元素&#xff0c;不覆盖原数据&#xff0c;原数据向后顺延 4、pop() …

script webshell jspWebShell / pythonWebShell / phpWebShell

google shell http://www.jiunile.com/google-shell.html http://goosh.org用命令行来玩google , 输入ls可以看目录内容. shell爱好者们可以来玩下 http://simplejee.group.iteye.com/group/wiki/2105-simplejee simplejee是一个开源的面向j2ee初学者的小项目。该项目里提供…

Tomcat访问日志分析

常使用web服务器的朋友大都了解&#xff0c;一般的web server有两部分日志&#xff1a; 一是运行中的日志&#xff0c;它主要记录运行的一些信息&#xff0c;尤其是一些异常错误日志信息 二是访问日志信息&#xff0c;它记录的访问的时间&#xff0c;IP&#xff0c;访问的…

RESTful Web Services Cookbook中文版译者序

从去年开始我一直在翻译ORelly的《RESTful Web Services Cookbook 》&#xff0c;翻译的过程有些纠结&#xff0c;导致整本书的进度比预期的要慢很多&#xff0c;但一切原因都不能影响翻译的质量&#xff0c;我依然坚持这样一个原则。再过一段时间这本书就能与读者见面了&#…

project site_dns

在国内网上我看到的最多的内容: http://blog.sina.com.cn/s/blog_40488d6d0100052w.html The connection was reset The connection to the server was reset while the page was loading. * The site could be temporarily unavailable or too busy. Try again …