springmvc的contronller之间携带参数的跳转
来源:互联网 发布:淘宝的售后服务是什么 编辑:程序博客网 时间:2024/05/22 07:04
http://blog.csdn.net/qq_36769100/article/details/70886951
1、首先说一下不带参数的重定向格式:
- @RequestMapping(“/controller”)
- public String toController(){
- return “redirect:otherController.do”;//重定向跳转到其他controller
- }
- @RequestMapping(“/otherController”)
- public String toOtherController(String param,Model model){
- return “success”; //第二个controller负责跳转到页面
- }
这是不带参数的重定向,这种情况一般很少出现。
注意:
这里需要强调一下路径问题,上图实例中跳转到的controller是同一个anction下,路径中貌似是不需要带”/”的。至于跳转到其他action中的controller,路径可能会有不同。
2、重定向带参数的形式(get)
springmvc中有一个RedirectAttributes的类,这个类可以帮我们实现参数的传递:
RedirectAttributes.addAttribute(“参数名1”,参数1);
RedirectAttributes.addAttribute(“参数名2”,参数2);
然后重定向。
上代码:
- @RequestMapping(“/controller”)
- public String toController(RedirectAttributes attr){//第一个controller中传入RedirectAttributes参数
- attr.addAttribute(”param”,“test”); //封装跳转是需要携带的参数
- return “redirect:otherController.do”;
- }
- @RequestMapping(“/otherController”)
- public String toOtherController(String param,Model model){//这里传入param需要和attr中的key一样
- System.out.println(”param—–:”+param);//这里输出的为:”param—–:test”
- model.addAttribute(”param1”, param); //Model封装参数,携带到前台
- return “success1”;
- }
这种方式相当于系统帮我们在重定向路径的后面携带了参数,类似于:
Return redirect:***.do?参数名1=参数1&参数名2=参数2;
注意:
这种提交方式采用的get,重定向的时候会把参数暴露在网址上,不安全,不建议使用。
2、重定向带参数的形式(隐藏参数)
SpringmvcRedirectAttribute中还有一个addFlashAttribute的方法,这个方法可以实现隐藏信息。
RedirectAttribute.addFlashAttribute(“参数名,参数);
但是使用addFlashAttribute的时候,在目标controller方法的参数列表中需要增加一个注解:
@ModelAttribute(“参数名”)String param.
上代码:
- @RequestMapping(“/controller2”)
- public String toController2(RedirectAttributes attr){//依然需要传入RedirectAttributes 参数
- attr.addFlashAttribute(”param”, “test”); //使用的是addFlashAttribute方法
- attr.addFlashAttribute(”param1”, “test1”);
- return “redirect:otherController2.do”;
- }
- @RequestMapping(“/otherController2”)
- public String totoOtherController2(@ModelAttribute(“param”)String param){//需要添加注解@ModelAttribute
- System.out.println(”param———-:”+param);//框架会自动帮我们获取
- return “success2”;
- }
通过这种方式,跳转过程中携带的参数,就会被隐藏掉。
- springmvc的contronller之间携带参数的跳转
- springmvc的contronller之间携带参数的跳转
- springmvc的contronller之间携带参数的跳转
- 页面之间的跳转并携带数据
- 安卓开发:两个活动之间携带数据的跳转
- Android获取跳转URI及携带的参数
- 关于springmvc重定向如何携带参数的问题
- SpringMVC几种重定向以及携带参数的处理
- 页面跳转参数携带与参数接收的几种方式
- springmvc的 action之间跳转解决方法
- springmvc中controller之间的跳转
- springmvc中方法之间的跳转。
- Android 从app 携带参数 跳转到其他app的指定界面
- 页面之间相互跳转的参数传递
- html获取url携带的参数
- url携带的参数转成json格式
- UIWebView携带参数的POST请求
- angularJS1 url中携带参数的获取
- JAVA面向对象编程艺术与思想:打印日历1
- JS实现类似Android吐司样式(Toast样式)瞬时弹出对话框
- Codeforces 263D Cycle in Graph【Dfs】
- 第二章 身份验证
- Apache 服务器部署拥有域名的虚拟服务器vhost
- springmvc的contronller之间携带参数的跳转
- 【cesium】obj转换为gltf方法
- Android studio快速集成Kotlin
- JAVA时间字符串去空格、冒号和横杠
- linux下配置Java开发环境
- 2.CSS背景
- Field 'id' doesn't have a default value
- 侧滑
- AngularJS对表格的增删改查(仅限前端)