zoukankan      html  css  js  c++  java
  • spring mvc redirect 重定向 跳转并传递参数

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

    公用代码:

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

  • 相关阅读:
    动态添加元素在ie中的事件(eg:onclick)无反应
    c# NOPI导出excel后 “发现有部分内容有问题 ”
    js中使用类和数组
    1.0 c#设计模式 --单例模式
    请求接口帮助类封装
    vs2017运行网站,代码停止,浏览器页面关闭问题解决
    无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动
    第8课 商业需求文档(BRD)撰写方法与技巧
    第7课 产品经理专业技能之 PRD/BRD/MRD文档撰写
    第六课 产品经理的知识管理
  • 原文地址:https://www.cnblogs.com/jpfss/p/9542909.html
Copyright © 2011-2022 走看看