实现登录后跳转到登录前页面时,发现用model.addAttribute()再redirect,跳转后URL会附上参数。
如 model.addAttribute("msg", "用户名或密码错误");
跳转后会是 地址?msg=用户名或密码错误
若想去掉,可以使用RedirectAttributes的addFlashAttribute方法。
public String login(HttpServletRequest request,Model model, RedirectAttributes redirectAttr){
//登录验证..
redirectAttr.addFlashAttribute("msg", msg);
return "redirect:xxx";
}jsp就可以用request.getAttribute("msg")来获取,页面再刷新会返回null。
本文介绍了在Spring MVC中如何处理登录后的页面跳转及如何避免URL携带敏感参数的问题。通过使用RedirectAttributes的addFlashAttribute方法,可以在重定向时传递一次性消息而不会出现在URL中。
3493

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



