您的位置:首页 > 编程语言 > Java开发

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

2015-10-31 12:58 323 查看

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

 @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"/>

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: