controller跳转controller
来源:互联网 发布:java中的方法 编辑:程序博客网 时间:2024/05/20 02:30
1.不带参数跳转
1.1
同一个controller内部跳转
return "redirect:/toList"; //可以省略一层路径等价于 return new ModelAndView("redirect:/toList");
1.2
不同controller内部跳转
return return "redirect:/MESBcElectric/list"; //两层路径必须齐全
2.拼接参数跳转
2.1 自己拼接参数
RedirectAttributes attr
return "redirect:/MESBcElectric/list?name="+name+"&password="+password;//自己在路径后拼接需要传递到另一个controller中的参数
缺点:自己拼接的参数,若是中文,会出现乱码
2.2 利用类拼接参数
RedirectAttributes attr;
attr.addAttribute(“name”, value);
public String findlist(MESBcElectric mESBcElectric,RedirectAttributes attr){ //自动拼接参数 attr.addAttribute("process", mESBcElectric.getProcess()); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM"); Date date1 = mESBcElectric.getDate(); String date=sdf.format(date1); attr.addAttribute("date",date);//将date由日期类型转化为字符串类型再进行拼接 return "redirect:/MESBcElectric/list"; 跳转后链接地址为:return "redirect:/MESBcElectric/list?process="+mESBcElectric.getProcess()+"&date="+date;
利用类拼接的参数,不会出现中文乱码
3.带参数不拼接url页面
RedirectAttributes attr;
attr.addFlashAttribute(“name”, form.getName());
页面取值,直接用el表达式就能获得到,这里的原理是放到session中,session在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢掉。
@RequestMapping("/save") public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr) throws Exception { String code = service.save(form); attr.addFlashAttribute("name", form.getName());//暂时存在session中 attr.addFlashAttribute("success", "添加成功!"); return "redirect:/index"; } @RequestMapping("/index") public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr) throws Exception { return "redirect:/main/list"; }
注意
attr.addAttribute(“a”, “a”); 和 attr.addFlashAttribute(“b”, “b”);的区别
1.使用RedirectAttributes的addAttribute方法传递参数会跟随在URL后面 ,如上代码即为http:/index.action?a=a 。
2.使用addFlashAttribute不会跟随在URL后面,会把该参数值暂时保存于session,待重定向url获取该参数后从session中移除,这里的redirect必须是方法映射路径,jsp无效。你会发现redirect后的jsp页面中b只会出现一次,刷新后b再也不会出现了,这验证了上面说的,b被访问后就会从session中移除。对于重复提交可以使用此来完成.
重要知识
通过Controller跳转controller所带的参数会自动封装在下一个Controller的实体类对象中,无需再进行request获取
- controller跳转controller
- Controller与Controller之间跳转
- controller跳转另外一个controller
- MVC只Controller跳转
- spring MVC controller 跳转
- controller内部跳转
- 通用页面跳转Controller
- Ajax请求Controller跳转
- spring mvc controller跳转
- controller控制页面跳转
- controller 之间的跳转
- controller 带参数跳转
- SpringMVC从Controller跳转到另一个Controller
- SpringMVC从Controller跳转到另一个Controller
- SpringMVC从Controller跳转到另一个Controller
- SpringMVC从Controller跳转到另一个Controller
- SpringMVC从Controller跳转到另一个Controller
- SpringMVC从Controller跳转到另一个Controller
- Android 本地时间/时区自动更新 -- NITZ
- hash表(散列表)详解
- 判断字符串中是否包含特定字符的方法总结
- 100种数据恢复的方法?!
- Android spinner点击相同选项处理无法响应事件问题,自定义spinner
- controller跳转controller
- Spring framework(1):框架构建&配置文件
- Leetcode:728. Self Dividing Numbers
- poj2965 The Pilots Brothers' refrigerator(枚举)
- cursor取值
- 【Struts2】8.OGNL
- 软阈值(Soft Thresholding)函数解读
- 如何在安装Linux之后自己修改Swap分区大小
- 【Tensorflow】tf.concat函数