spring mvc controller间跳转 重定向 传参
来源:互联网 发布:十大网络主播 编辑:程序博客网 时间:2024/05/06 09:10
文章出自:http://blog.csdn.net/jackpk/article/details/19121777
1. 需求背景
需求:spring MVC框架controller间跳转,需重定向。有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示。
2. 解决办法
需求有了肯定是解决办法了,一一解决,说明下spring的跳转方式很多很多,我这里只是说一些自我认为好用的,常用的,spring分装的一些类和方法。
(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在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢掉。
3. 总结
最底层还是两种跳转,只是spring又进行了封装而已,所以说跳转的方式其实有很多很多种,你自己也可以封一个,也可以用最原始的response来,也没有问题。好了,就到这儿。
其实也没有什么,但是知道了这个就很简单了,之前没搞懂,现在搞懂了,和大家分享。有问题的给我留言。
spring mvc3中的addFlashAttribute方法
- //第三个参数(UserModel user)默认为绑定对象
- @RequestMapping(value = "/user/save", method = RequestMethod.POST)
- public ModelAndView saveUser(HttpServletRequest request, HttpServletResponse response,UserModel user) throws Exception {
- ModelAndView mv = new ModelAndView("/user/save/result");//默认为forward模式
- // ModelAndView mv = new ModelAndView("redirect:/user/save/result");//redirect模式
- mv.addObject("message","保存用户成功!");
- return mv;
- }
- @RequestMapping(value = "/user/save", method = RequestMethod.POST)
- public ModelAndView saveUser(UserModel user, RedirectAttributes redirectAttributes) throws Exception {
- redirectAttributes.addFlashAttribute("message", "保存用户成功!");//使用addFlashAttribute,参数不会出现在url地址栏中
- return "redirect:/user/save/result";
- }
- <form:form id="myform" action="saveUserDetails.action" method="POST" commandName="user">
- <form:input type="text" name="firstName" path="firstName"/>
- <form:input type="text" name="lastName" path="lastName"/>
- <form:input type="text" name="email" path="email"/>
- <input type="submit" value="submit">
- </form:form>
- @RequestMapping(value="/saveUserDetails.action", method=RequestMethod.POST)
- public String greetingsAction(@Validated User user,RedirectAttributesredirectAttributes){
- someUserdetailsService.save(user);
- redirectAttributes.addFlashAttribute("firstName", user.getFirstName());
- redirectAttributes.addFlashAttribute("lastName", user.getLastName())
- return "redirect:success.html";
- }
- success.html:
- <div>
- <h1>Hello ${firstName} ${lastName}. Your details stored in our database.</h1>
- </div><br>
- @RequestMapping(value="/success.html", method=RequestMethod.GET)
- public String successView(HttpServletRequest request){
- Map<String,?> map = RequestContextUtils.getInputFlashMap(request);
- if (map!=null)
- return "success";
- else return "redirect:someOtherView"; //給出其他提示信息
spring mvc 如何请求转发和重定向呢?
url: http://blog.sina.com.cn/s/blog_9cd9dc7101016abw.html
往下看:
由于这部分内容简单,一带而过了。
1.请求转发:
public ModelAndView testForward(ModelAndView
}
(2)返回字符串
2.请求重定向
(1)带参数:
- @RequestMapping(value="/redirect",method=RequestMethod.GET)
- public
String testRedirect(RedirectAttributes attr){ -
attr.addAttribute("a", "a"); -
attr.addFlashAttribute("b", "b"); -
return "redirect:/index.action"; - }
p:ignoreDefaultModelOnRedi
(2)无参数
public String testRedirect(){
return "redirect:/index.action";
}
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc Controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- spring mvc controller间跳转 重定向 传参
- 智能路由器摸索
- spring boot 启动web,内嵌tomcat
- Msp430 bsl program
- Impala配置与错误解决方案
- 行人检测
- spring mvc controller间跳转 重定向 传参
- ubuntu12.04下安装QT4
- Node.js笔记
- 巴巴运动网功能设计
- 两台Ubuntu计算机利用Synergy共享鼠标和键盘
- 心理学与生活《感知与记忆》
- AChartEngine中的Renderer和DataSet介绍
- 大型网站架构技术一览
- java小Tips之 长整型变量标示字母要用大写避免歧义