Spring mvc框架 controller间跳转 ,重定向 ,传参
一、需求背景
@RequestMapping(value = "/activityType", method = RequestMethod.GET)
public String activityType(HttpServletRequest request, ModelMap model,RedirectAttributes attr) throws Exception{
logger.info("into activityType");
String id = request.getParameter("typeId");
String activityId = request.getParameter("activityId");
// model.put("typeId", id);
// model.put("activityId",activityId); // attr.addFlashAttribute("typeId", id);
// attr.addFlashAttribute("activityId", activityId); attr.addAttribute("typeId", id);
attr.addAttribute("activityId", activityId); ActivityTypeInfo activityTypeInfo = activityTypeInfoBiz.get(Integer.valueOf(id));//根据活动ID查询活动类型对象
Integer activityType=activityTypeInfo.getType();
switch(activityType){//根据类型不同跳转不同的页面
case 1:
System.out.println("线上吸粉活动!");
return "redirect:/active/xsxf/baseInfo";
case 2:
System.out.println("你答我奖活动!");
return "forward:/active/ndwj/baseInfo";
default :
return "index";
} }
二、 解决办法
1. 我在后台一个controller跳转到另一个controller,为什么有这种需求呢,是这样的。我有一个列表页面,然后我会进行新增操作,新增在后台完成之后我要跳转到列表页面,不需要传递参数,列表页面默认查询所有的。
方式一:使用ModelAndView
return new ModelAndView("redirect:/toList");
这样可以重定向到toList这个方法
方式二:返回String
return "redirect:/ toList ";
其它方式:其它方式还有很多,这里不再做介绍了,比如说response等等。这是不带参数的重定向。
2. 第二种情况,列表页面有查询条件,跳转后我的查询条件不能丢掉,这样就需要带参数的了,带参数可以拼接url
方式一:自己手动拼接url
new ModelAndView("redirect:/toList?param1="+value1+"¶m2="+value2);
这样有个弊端,就是传中文可能会有乱码问题。
方式二:用RedirectAttributes 。这个是发现的一个比较好用的一个类 。这里用它的addAttribute方法,这个实际上重定向过去以后你看url,是它自动给你拼了你的url。
使用方法:
attr.addAttribute("param", value);
return "redirect:/namespace/toController";
这样在toController这个方法中就可以通过获得参数的方式获得这个参数,再传递到页面。过去的url还是和方式一一样的。
3. 带参数不拼接url页面也能拿到值(重点是这个)一般我估计重定向到都想用这种方式:
@RequestMapping("/save")
public String save(@ModelAttribute("form") Bean 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:/maintenance/toAddConfigCenter";
}
}
@RequestMapping("/index")
public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr) throws Exception {
return "redirect:/main/list";
}
页面取值不用我说了吧,直接用el表达式就能获得到,这里的原理是放到session中,session在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢掉。
注意: attr.addAttribute("a", "a"); 和 attr.addFlashAttribute("b", "b"); 的区别。
带参数可使用RedirectAttributes参数进行传递:
注意:
1.使用RedirectAttributes的addAttribute方法传递参数会跟随在URL后面 ,如上代码即为http:/index.action?a=a 。
2.使用addFlashAttribute不会跟随在URL后面,会把该参数值暂时保存于session,待重定向url获取该参数后从session中移除,这里的redirect必须是方法映射路径,jsp无效。你会发现redirect后的jsp页面中b只会出现一次,刷新后b再也不会出现了,这验证了上面说的,b被访问后就会从session中移除。对于重复提交可以使用此来完成.
另外,如果使用了RedirectAttributes作为参数,但是没有进行redirect呢?这种情况下不会将RedirectAttributes参数传递过去,默认传forward对应的model,官方的建议是:
p:ignoreDefaultModelOnRedirect="true" />
设置下RequestMappingHandlerAdapter 的ignoreDefaultModelOnRedirect属性,这样可以提高效率,避免不必要的检索。
Spring mvc框架 controller间跳转 ,重定向 ,传参的更多相关文章
- spring mvc controller间跳转 重定向 传参 (转)
转自:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景 需求:spring MVC框架contr ...
- Spring Mvc Controller间跳转 重定向 传参 (转)
原文链接:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景 需求:spring MVC框架con ...
- spring mvc controller间跳转 重定向 传参(转)
spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...
- spring mvc controller间跳转 重定向 传参
http://blog.csdn.net/jackpk/article/details/19121777/
- spring mvc controller间跳转 重定向
1. 需求背景 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...
- springMVC controller间跳转 重定向 传递参数的方法
springMVC controller间跳转 重定向 传递参数的方法 spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参 ...
- flutter页面间跳转和传参-Navigator的使用
flutter页面间跳转和传参-Navigator的使用 概述 flutter中的默认导航分成两种,一种是命名的路由,一种是构建路由. 命名路由 这种路由需要一开始现在创建App的时候定义 new M ...
- jsp与spring mvc后台controller间参数传递处理之总结
在编程过程中,最容易出现问题及卡壳的地方,往往是各层之间接缝处,接缝处往往存在着各种各样的参数传递,数据转换和格式化,参数很好的传递并正确接收过来之后就是复杂逻辑之间的处理了,所以为了避免多种问题占用 ...
- Flutter 路由 页面间跳转和传参 返回
Navigator Navigator用来管理堆栈功能(即push和pop),在Flutter的情况下,当我们导航到另一个屏幕时,我们使用Navigator.push方法将新屏幕添加到堆栈的顶部.当然 ...
随机推荐
- Yii2.0学习笔记:第一个页面Saying Hello
Controller目录下创建TestController.php 复制下面代码,yii跟tp一样,全程命名空间 <?php namespace app\controllers; use yii ...
- redis持久化机制
redis持久化 redis的数据存在内存中,所以存取性能好.但是存在内存中的数据存在一个问题,一旦机器重启,内存数据消失.为了解决这个问题,redis支持持久化.持久化就是为了解决内存数据丢失时恢复 ...
- 高性能 Windows Socket 组件 HP-Socket v3.0.2 正式发布
HP-Socket 是一套通用的高性能 Windows Socket 组件包,包含服务端组件(IOCP 模型)和客户端组件(Event Select 模型),广泛适用于 Windows 平台的 TCP ...
- GJM : Python简单爬虫入门(二) [转载]
感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...
- gcc shared object
介绍一个生成动态链接库*.so的例子: 首先新建1个头文件test.h: #include <stdio.h> void first(); void second(); void thir ...
- 七个结构模式之装饰者模式(Decorator Pattern)
定义: 使用组合的方法,动态给一个类增加一些额外的功能,避免因为使用子类继承而导致类继承结构复杂.并且可以保持和被装饰者同一个抽象接口,从而使客户端透明. 结构图: Component:抽象构件类,定 ...
- Select-or-Die:灵活的 jQuery 下拉列表插件
Select-or-Die 是一个 jQuery 插件,用来自定义下拉列表(Select)元素.原生的下拉选择元素在各个浏览器的默认样式差异很多,而且自定义样式很困难,因此 Web 开发人员喜欢使用插 ...
- [GitHub] GitHub Repositories
https://github.com/karqical2016/karqical2016.github.io.git https://github.com/karqical2016/java.git ...
- 图文详细解说DevExpress 2015新版亮点【附文档下载】
历时两个月,翻译自官网的更新说明文档,最后整理成册,文末附PDF地址. DevExpress 在今年6月份发布了正式的2015版本,2015在旧版基础上有明显的升级改进,尤其是DevExtreme.W ...
- SharePoint 2010: Export User Profile Properties to a Text File or Excel using PowerShell
导出到txt [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server") ...