在我的 Spring 中,我使用 RedirectAttributes 在登录成功或失败后显示消息,并且我有一个 SessionAttribute。我设置了两次 SessionAttributes 第一次是当我单击菜单时我将对象设置为 SessionAttribute 第二次是在登录成功后我的问题是我在登录成功后设置的 SessionAttribute 不起作用如果我删除 RedirectAttributes 意味着它正在工作美好的。
当我搜索时,我发现了这个
addFlashAttribute actually stores the attributes in a flashmap (which is internally maintained in the
users session and removed once the next redirected request gets fulfilled)
当我调试它时,它总是显示默认值,而不显示登录后设置的值。
这是用于登录
@RequestMapping(value = "/sample-client", method = RequestMethod.POST)
public String getClient(HttpServletRequest request, Model model, final RedirectAttributes redirectAttributes) {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
Client client = createClient(userName, password);
if (client != null) {
model.addAttribute("clientObject", client);
redirectAttributes.addFlashAttribute("message", "Logined Successfully");
return "redirect:" + "/sample/user";
} else {
redirectAttributes.addFlashAttribute("error", "true");
return "redirect:" + "/sample/login";
}
}
after login
@RequestMapping(value = "/byName", method = RequestMethod.GET)
public
@ResponseBody
String getUserByName(HttpServletRequest request, @ModelAttribute("clientObject") User user) {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
Integer page = Integer.parseInt(request.getParameter("page"));
return sample.getUserSearchByName(user, firstName, lastName, page);
}
当我在这里检查 clientObject 时,它显示了默认值。