问题
In my Spring i have used RedirectAttributes to display the message after login success or failure and i have one SessionAttribute. I set SessionAttributes in two times first one is when i click the menu i set Object to SessionAttribute and second one is after login success what my problem is the SessionAttribute which i set after login success is not working if i remove the RedirectAttributes means it is working fine.
when i searched i found this
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)
when i debug it alwys shows the default one not show the value set after login.
this is for login
@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);
}
when i checked the clientObject here it shows the default one.
来源:https://stackoverflow.com/questions/17852921/spring-redirectattributes-clears-session-after-redirect