在项目中做form表单功能提交时,防止用户客户端后退或者刷新时重复提交问题,需要在服务端进行重定向跳转,具体跳转方式有以下几种方式:
公用代码:
@RequestMapping(value="/index",method = { RequestMethod.POST, RequestMethod.GET }) public ModelAndView index(HttpServletResponse response){ ModelAndView model = new ModelAndView("/home/index"); return model; }
一、使用HttpServletResponse 进行重定向跳转
@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直接跳转
@RequestMapping(value="/toIndex",method = { RequestMethod.POST, RequestMethod.GET }) public String toIndex(HttpServletResponse response){ return "redirect:/index"; }
注:当需要传递简单参数时可以使用以上两种方式通过get方式将参数拼接到url路劲后面。
三、依赖Spring mvc的RedirectAttributes
@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 可以直接使用${userName},${userPass}来获取重定向跳转的参数信息,这种方式可以处理复杂的参数传值问题,还可以使用此种方式来隐藏或缩短原有请求URL信息。
在controller中获取放在RedirectAttributes中的userName信息的方式:
@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 controller间跳转 重定向 传参
spring mvc重定向导致内存溢出解决方案
Spring MVC属于SpringFrameWork的后续产品,已经融合在..., 精通Spring MVC4zui适合已经熟悉Spring编程基础知识并迫切希望扩展其Web技能的开发人员。通过阅读本书,读者将深度把握Spring MVC的各项特性及实用技巧。
前端使用jquery向后台传递数组类型的参数,Java后台直接通过List类型接收,会发现无法取到参数。
3.Spring MVC 请求参数的获取案例 4.Spring MVC 域对象共享数据案例 5.Spring MVC @ModelAttribute 注解案例 6.Spring MVC 国际化案例 7.Spring MVC 请求转发与请求重定向案例 8.Spring MVC 下载 Excel 文档的需求...
十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八、spring mvc 多视图控制器 十九、 <mvc:annotation-...
十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八、spring mvc 多视图控制器 十九、 <mvc:annotation-...
本文详细介绍spring MVC的原理和开发心得体会。
NULL 博文链接:https://shmilyaw-hotmail-com.iteye.com/blog/2246344
精通Spring MVC 4 中文
Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...
java spring mvc项目,通过URL地址传递对象参数给action
第3篇是Spring MVC技术入门,包括Spring MVC的背景介绍、架构整体剖析、环境搭建、处理器与映射器的讲解、前端控制器的源码分析、多种视图解析器的介绍、请求映射与参数绑定的介绍、Validation校验与异常处理和拦截...
十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八、spring mvc 多视图控制器 十九、 <mvc:annotation-...
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...
Spring MVC--6.封装参数获得与传递
Spring MVC所需jar包,包含java开发中 Spring MVC架构中最常用的jar包
关于构建Sping MVC的Jar包,包括Sping2.5.6和Hibernate3.6.8