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

本文介绍了一个简单的退出登录方法实现,通过清除 session 中的用户名属性并重定向到登录页面来完成退出操作。针对重定向后地址栏出现参数的问题,文章提供了在 Spring MVC 配置文件中设置 ignoreDefaultModelOnRedirect 为 true 的解决方案。

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值