今天遇到一个应用场景:
在需要在自定义的Interceptor中判断用户密码是否过期,如果过期,则重定向到修改密码页,强制修改密码,同时给出提示:“您的密码已过期,请修改密码”
判断逻辑很简单,但是重定向的时候需要前台有消息提示,如果是在Controller中,可以在方法上注入RedirectAttributes
参数,但是Interceptor中默认没有这个参数,那么我们如何实现RedirectAttributes
的flashMessage功能呢?
通过跟踪Spring源码,发现了实现方法:
FlashMap flashMap = new FlashMap();
flashMap.put('warning', "密码已过期,请先修改密码!");
FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
flashMapManager.saveOutputFlashMap(flashMap, request, response);
搞定!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有