程序中需要一个退出登录的方法, 代码:
@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"/>
本文介绍了一个简单的退出登录方法实现,通过清除 session 中的用户名属性并重定向到登录页面来完成退出操作。针对重定向后地址栏出现参数的问题,文章提供了在 Spring MVC 配置文件中设置 ignoreDefaultModelOnRedirect 为 true 的解决方案。
1176

被折叠的 条评论
为什么被折叠?



