Spring mvc Controller间跳转/重定向/传参
Spring mvc Controller常用写法
1.ModelAndView
@RequestMapping(value = "/getxxxList.html")
public ModelAndView getxxxList(XxxDTO xxxDTO,WebPage webPage){//ModelAndView modelAndView = new ModelAndView("/xxx/xxxList");//跳转ModelAndView modelAndView = new ModelAndView("redirect:/xxx/xxxList");//重定向try{//检索参数回显modelAndView.addObject("xxxDTO",xxxDTO);//执行查询modelAndView.addObject("xxxList",xxxList);}catch (Exception e){e.printStackTrace();}return modelAndView;
}
2.String
@RequestMapping(value = "/getxxxList.html")
public String getxxxList(XxxDTO xxxDTO,WebPage webPage,Model model){try{//检索参数回显model.addAttribute("xxxDTO",xxxDTO);//执行查询model.addAttribute("xxxList",xxxList);}catch (Exception e){e.printStackTrace();}//return "/xxx/xxxList";//跳转return "redirect:/xxx/xxxList";//重定向
}
返回地址参数拼接
1.手动拼接URL
"redirect:/xxx/xxxList?param1="+value1+"¶m2="+value2"
2-1.使用RedirectAttributes自动拼接重定向URL
@RequestMapping(value = "/getxxxList.html")
public String getxxxList(RedirectAttributes redirectAttributes){redirectAttributes.addAttribute("param1", value1);redirectAttributes.addAttribute("param2", value2);return "redirect:/xxx/toController";//重定向
}
Tip:曾经在项目中遇到过很诡异的重定向问题,业务代码执行无错无异常并且顺利到达 return,重定向结果就是页面报错500,后台并未有异常抛出,且在众多Controller方法中,只有两三个Controller方法遇到了这样的问题,使用RedirectAttributes后解决了这个重定向异常的问题。
2-2.使用RedirectAttributes的addFlashAttribute方法
@RequestMapping("/save")
public String save(@ModelAttribute("form") XxxBean form,RedirectAttributes attr)throws Exception {String code = service.save(form);if(code.equals("000")){attr.addFlashAttribute("name", form.getName()); attr.addFlashAttribute("success", "添加成功!");return "redirect:/index";}else{attr.addAttribute("projectName", form.getProjectName()); attr.addAttribute("enviroment", form.getEnviroment()); attr.addFlashAttribute("msg", "添加出错!错误码为:"+rsp.getCode().getCode()+",错误信息为:"+rsp.getCode().getName());return "redirect:/xxx/toController";}
}
注意:
- 在2-1中使用addAttribute方法传参,参数会自动拼接在URL后面,而使用addFlashAttribute方法会把参数值暂存于session,待重定向URL获取该参数后从session中移除,这里的redirect必须是方法映射路径,jsp无效。重定向后参数值只会出现一次,刷新页面后不再出现。
- 如果使用了RedirectAttributes作为参数,但是没有进行redirect,这种情况下不会将RedirectAttributes参数进行传递,默认还是传递forward对应的model,官方的建议是可以设置RequestMappingHandlerAdapter的ignoreDefaultModelOnRedirect属性,这样可以提高效率,避免不必要的检索。
Spring mvc Controller间跳转/重定向/传参相关推荐
- spring mvc controller间跳转 重定向 传参
spring mvc controller间跳转 重定向 传参 1. 需求背景 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形 ...
- ajax redirectattributes 使用,Spring中RedirectAttributes对象重定向传参
Spring3中的FlashAttribute 为 了防止用户刷新重复提交,save操作之后一般会redirect到另一个页面,同时带点操作成功的提示信息.因为是Redirect,Request里 的 ...
- Spring MVC Controller 要点
2019独角兽企业重金招聘Python工程师标准>>> 今天看到一篇讲解 Spring MVC Controller 的文章,比较详细,顺道翻译下. 在 Spring MVC 中,我 ...
- java语言如何跳转界面_在java中spring mvc页面如何跳转,详细图解
众所周知,java编程语言的应用在当今社会必不可少,它是当下人们提升自己充实自身的重要选择.所以,今天主要来为大家介绍java的入门知识,在 java中spring mvc页面如何跳转,通过详细的图片 ...
- Spring MVC Controller与jquery ajax请求处理json
在用 spring mvc 写应用的时候发现jquery传递的[json数组对象]参数后台接收不到,多订单的处理,ajax请求: var cmd = {orders:[{"storeId&q ...
- Spring MVC更多家族成员----文件上传---06
Spring MVC更多家族成员----文件上传---06 本节导读 文件上传与MultipartResolver 使用MultipartResolver进行文件上传的简单分析 StandardSer ...
- java注解返回不同消息,Spring MVC Controller中的一个读入和返回都是JSON的方法如何获取javax.validation注解的异常信息...
Spring MVC Controller中的一个读入和返回都是JSON的方法怎么获取javax.validation注解的错误信息? 本帖最后由 LonelyCoder2012 于 2014-03- ...
- Spring MVC Controller中返回json数据中文乱码处理
问题 在使用spring MVC Controller的过程中,发现返回到客户端的的中文出现乱码.后台Java代码: @RequestMapping(value = "/upload&quo ...
- SpringMVC重定向传参
2019独角兽企业重金招聘Python工程师标准>>> SpringMVC Controller间跳转,需重定向. 分三种情况:(1)不带参数跳转(2)带参数拼接url形式跳转(3) ...
最新文章
- 原来这样做运维,就可以不被 KO丨课程推广
- 拖动窗体的任意区域移动窗体
- PHP设计模式之适配器模式
- 是什么包_包粽子教程,喜欢的收藏,以后想吃什么样的都可以自己包
- 共享可写节包含重定位_未经许可成都街头现共享电动单车?哈啰出行:只是测试...
- 《你必须掌握的Entity Framework 6.x与Core 2.0》书籍出版
- 计算机如何输入ip地址,电脑如何切换ip地址_怎么让电脑切换ip地址-win7之家
- Tensorflow+gpu安装
- 循环序列模型 —— 1.12 深层循环神经网络
- Picasa2图片查找浏览工具
- TX2(linux系统、Ubuntu系统)输入法不显示拼音候选框、下拉框
- MPai数据科学平台
- vue 使用animate动画
- ktt算法 约化_答:那些深度学习《面试》你可能需要知道的
- 南大地球科学能转计算机类吗,南京大学地球科学系
- nginx+域名配置
- 为什么《请回答1988》能被称为神剧
- python网络爬虫学习的汇总
- 服务器拒绝了您发送离线文件的请求,处理qq发送离线文件被提示“服务器拒绝了您发送离线文件”的方法...
- 牛奶食疗可治多种疾病(图)
热门文章
- “比较级”和“最高级”的用法
- 伟大的牛逼之前都是傻逼式的坚持
- win10、win11中Elan触摸板滚动方向反转、启动“双指点击打开右键菜单“、“双指滚动“
- 特斯拉、百度、阿里趋之若鹜,无人驾驶卡车赛道现新风口
- Java编写“诸神黄昏“小游戏
- 常用超好用正则表达式!
- Echarts实现正弦曲线和散点图
- CSS学习笔记(详细,不定期更新)
- STM32 gcc编译环境搭建
- 重构系列之重构的标志:《重构》代码的坏味道