Spring MVC请求转发和重定向

1、请求重定向:// 重定向到toList请求

 

 <1> 不带参数的重定向
        方式一:使用ModelAndView
                    return new ModelAndView("redirect:/toList");
        方式二:返回String
                    return "redirect:/ toList"; 
 
 <2>  带参数 的重定向
           方式一:自己手动拼接url

                    return new ModelAndView("redirect:/toList?param1="+value1+"&param2="+value2);

                    弊端:传中文可能乱码

        方式二:用RedirectAttributes类
                      使用addAttribute方法,自动给你拼接url

                      使用方法:

                      public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr){

                          ...

                          attr.addAttribute("param", value);

                          return "redirect: /toList ";
                            }

                      在toList方法中可以通过获得参数的方式获取参数

 

 

2、请求转发:// 转发到toList请求
 
<1> 不带参数的转发
        方式一:使用ModelAndView
                    return new ModelAndView(" forward: /toList");
        方式二:返回String
                    return " forward: /toList "; 
 
  <2> 带参数的转发
        方式一:使用ModelAndView
                    return new ModelAndView(" forward :/toList ?param1="+value1+"&param2="+value2 ");
        方式二:返回String
                    return " forward :/ toList ?param1="+value1+"&param2="+value2 "; 

 

 

 转载请注明出处: http://xieke90.iteye.com/blog/2255633

 

猜你喜欢

转载自xieke90.iteye.com/blog/2255633