在我的Spring中,我使用RedirectAttributes在登录成功或失败后显示消息 我有一个SessionAttribute。我在两次设置SessionAttributes时首先是单击菜单 我将Object设置为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)
当我调试它时,alwys显示默认值不会显示登录后设置的值。
这是登录
@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时,它会显示默认值。