SpringMVC中 Redirect后地址栏带参数, 怎么把它去掉.

程序中需要一个退出登录的方法, 代码:

 @RequestMapping(value="/logout", method=RequestMethod.GET)
 public String Logout( HttpSession session){
  if(null != session.getAttribute(LOGIN_USERNAME)){
   String username = session.getAttribute(LOGIN_USERNAME).toString();
   session.removeAttribute(LOGIN_USERNAME);
  }
 
  return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "login";
 }
 
这个退出登录的方法, 目标是将session中的用户名删除, 再重定向到登录界面就可以了. 但重定向后地址栏里是有参数的(形如 login?loginUsername=adm 这种).

原因:
官方文档, The RequestMappingHandlerAdapter provides a flag called "ignoreDefaultModelOnRedirect" that can be used to indicate the content of the default Model should never be used if a controller method redirects. Instead the controller method should declare an attribute of type RedirectAttributes or if it doesn’t do so no attributes should be passed on to RedirectView. Both the MVC namespace and the MVC Java config keep this flag set to false in order to maintain backwards compatibility. However, for new applications we recommend setting it to true

解决方法: springMVC的配置文件中注解开关中配置"ignoreDefaultModelOnRedirect"变量为true, 如下:
<mvc:annotation-driven ignoreDefaultModelOnRedirect="true"/>

猜你喜欢

转载自asjava.iteye.com/blog/2253433