spring mvc 如何请求转发和重定向呢?往下看:
由于这部分内容简单,一带而过了。
<wbr></wbr>
1.请求转发:
<wbr><wbr>(1)返回<span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; background-color:rgb(250,250,250)">ModelAndView</span><span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; background-color:rgb(250,250,250)"><wbr>:</wbr></span></wbr></wbr>
@RequestMapping(value="/model",method=RequestMethod.GET)
public ModelAndView testForward(ModelAndView <wbr><wbr>model,@RequestParam(value="id",defaultValue="1",required=false)Long id){<br><wbr><wbr><wbr>User u = getBaseService().get(User.class, id);<br><wbr><wbr><wbr>model.addObject("user", u);<br><span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; color:rgb(255,0,0)"><wbr><wbr><wbr>model.setViewName("forward:index.jsp");</wbr></wbr></wbr></span><br><wbr><wbr><wbr>return model;<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr>如上代码,如果返回modelAndView 则可以如红色标注,添加forward即可,若想重定向,可把forward替换成redirect便可达到目的。</wbr>
<wbr></wbr>
(2)返回字符串
<wbr></wbr>
@RequestMapping(value="/forward",method=RequestMethod.GET)
<wbr><wbr>public String testForward(){<br><br><span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; color:rgb(255,0,0)"><wbr><wbr><wbr><wbr>return "forward:/index.action";</wbr></wbr></wbr></wbr></span><br><wbr><wbr>}</wbr></wbr></wbr></wbr>
<wbr>如上代码红色部分。</wbr>
<wbr></wbr>
2.请求重定向
<wbr>对于请求转发可以分为:1.带参数 2.不带参数</wbr>
<wbr></wbr>
(1)带参数:
<wbr></wbr>
Java代码<wbr><wbr><a target="_blank" href="http://blog.sina.com.cn/s/blog_9cd9dc7101016abw.html" title="收藏这段代码" style="text-decoration:none; color:rgb(30,123,88)"><img src="http://yjflfliulei.iteye.com/images/icon_star.png" alt="收藏代码" title="spring<wbr>MVC<wbr>3.1<wbr>forword/redirect" style="margin:0px; padding:0px; border:0px; list-style:none"></a></wbr></wbr>
- @RequestMapping(value="/redirect",method=RequestMethod.GET)<wbr><wbr></wbr></wbr>
- public<wbr>String<wbr>testRedirect(RedirectAttributes<wbr>attr){<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr>attr.addAttribute(<span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; color:blue">"a"</span>,<wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; color:blue">"a"</span>);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr>attr.addFlashAttribute(<span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; color:blue">"b"</span>,<wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; color:blue">"b"</span>);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; color:rgb(127,0,85)"><strong>return</strong></span><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; color:blue">"redirect:/index.action"</span>;<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- }<wbr><wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr>
<wbr>带参数可使用RedirectAttributes参数进行传递:</wbr>
<wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; color:rgb(255,0,0)"><wbr>注意</wbr></span>:<span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; background-color:rgb(255,255,153)">1.使用RedirectAttributes的addAttribute方法传递参数会跟随在URL后面 ,如上代码即为http:/index.action?a=a</span></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr>2.使用addFlashAttribute不会跟随在URL后面,会把该参数值暂时保存于session,待重定向url获取该参数后从session中移除,这里的redirect必须是方法映射路径,jsp无效。你会发现redirect后的jsp页面中b只会出现一次,刷新后b再也不会出现了,这验证了上面说的,b被访问后就会从session中移除。对于重复提交可以使用此来完成.</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr>另外,如果使用了RedirectAttributes作为参数,但是没有进行redirect呢?这种情况下不会将RedirectAttributes参数传递过去,默认传forward对应的model,官方的建议是:</wbr></wbr></wbr>
p:ignoreDefaultModelOnRedi<wbr>rect="true" /></wbr>
<wbr>设置下<span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; background-color:rgb(250,250,250)">RequestMappingHandlerAda<wbr>pter 的</wbr></span><span style="word-wrap:normal; word-break:normal; line-height:21.600000381469727px; background-color:rgb(250,250,250)">ignoreDefaultModelOnRedi<wbr>rect属性,这样可以提高效率,避免不必要的检索。</wbr></span></wbr>
<wbr></wbr>
<wbr></wbr>
(2)无参数
<wbr></wbr>
@RequestMapping(value="/redirect",method=RequestMethod.GET)
public String testRedirect(){
return "redirect:/index.action";
}