如题所示,在SpringMVC中可使用forward和redirect关键字在Controller中对原请求进行转发或重定向到其余的Controller。好比能够这样使用:html
package cn.zifangsky.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import cn.zifangsky.model.User; @Controller public class TestController { @RequestMapping("/user/index.html") public ModelAndView userIndex() { return new ModelAndView("user/index"); } @RequestMapping("/testForward.html") public ModelAndView testForward(@RequestParam("username") String username){ ModelAndView mAndView = new ModelAndView("forward:/user/index.html"); User user = new User(); user.setName(username); mAndView.addObject("user", user); return mAndView; } @RequestMapping("/testRedirect.html") public ModelAndView testRedirect(@RequestParam("username") String username){ ModelAndView mAndView = new ModelAndView("redirect:/user/index.html"); User user = new User(); user.setName(username); mAndView.addObject("user", user); return mAndView; } }
而后项目启动后,在浏览器中访问:http://localhost:9180/CookieDemo/testForward.html?username=forwardjava
页面显示效果以下:web
能够看出,在使用forward进行转发时请求的URL连接是不会改变的spring
接着,在浏览器中访问:http://localhost:9180/CookieDemo/testRedirect.html?username=redirect浏览器
页面显示效果以下:session
能够看出,在使用redirect进行重定向时请求的URL连接发生了改变,而且在controller中放置的参数并无传递进行。那么,若是想要在重定向时把参数也传递过去应该怎么作呢?app
示例代码以下:ide
@RequestMapping("/user/index.html") public ModelAndView userIndex(HttpServletRequest request) { ModelAndView mAndView = new ModelAndView("user/index"); HttpSession session = request.getSession(); mAndView.addObject("user", session.getAttribute("user")); session.removeAttribute("user"); return mAndView; } @RequestMapping("/testRedirect.html") public ModelAndView testRedirect(@RequestParam("username") String username,HttpServletRequest request){ ModelAndView mAndView = new ModelAndView("redirect:/user/index.html"); User user = new User(); user.setName(username); request.getSession().setAttribute("user", user); return mAndView; }
示例代码以下:spa
@RequestMapping("/user/index.html") public ModelAndView userIndex() { return new ModelAndView("user/index"); } @RequestMapping("/testRedirect.html") public ModelAndView testRedirect(@RequestParam("username") String username,RedirectAttributes redirectAttributes){ ModelAndView mAndView = new ModelAndView("redirect:/user/index.html"); User user = new User(); user.setName(username); // redirectAttributes.addAttribute("user", user); //URL后面拼接参数 redirectAttributes.addFlashAttribute("user", user); return mAndView; }
使用RedirectAttributes这个类来传递参数写法就很简单了,只须要在须要重定向的controller的方法参数中添加RedirectAttributes类型的参数,而后把须要重定向以后也可以获取的参数放进去便可orm
固然,听说RedirectAttributes本质上也是经过Session来实现的(PS:相关源代码我没看过,只能听说了)。实际上上面的代码是省略的写法,由于重定向以后就直接返回页面视图了,若是通过几回重定向的话估计上面那种写法就获取不到参数了,所以关于获取参数通常还有如下两种方式:
(1)使用@ModelAttribute注解获取参数:
@RequestMapping("/user/index.html") public ModelAndView userIndex(@ModelAttribute("user") User user) { ModelAndView mAndView = new ModelAndView("user/index"); mAndView.addObject("user", user); return mAndView; }
(2)使用RequestContextUtils类来获取:
@RequestMapping("/user/index.html") public ModelAndView userIndex(HttpServletRequest request) { ModelAndView mAndView = new ModelAndView("user/index"); Map<String, Object> map = (Map<String, Object>) RequestContextUtils.getInputFlashMap(request); User user = (User) map.get("user"); mAndView.addObject("user", user); return mAndView; }
总结,这两种获取方式均可以成功获取到参数。可是仍是略显麻烦,若是只是一次重定向以后就返回页面视图的话推荐使用最简单那种写法
PS:上面图片中的水印是我我的博客的域名,所以还请管理员手下留情不要给我标为“转载文章”,谢谢!!!