I'm new to spring. There is a Controlle called userController. In this controller there is method called signIn. In this method is user entered correct userid and password then page redirect to the same view with session values. Then user can check his profile details. For that i have created a method call account in the userController. In this method i can not get the previously set session values.How can i get it? This is my implementation of two method. this issignIn method
@RequestMapping(value = "/sign_in", method = RequestMethod.POST) public String signIn(@RequestHeader(value = "Accept") String headerAccept,@ModelAttribute User requestParamUser,RedirectAttributes redirectAttrs,HttpSession session) { JSONObject obj = new JSONObject(); try { // Check request parameters are null if ((requestParamUser.getUserId() == null)|| (requestParamUser.getPassword() == null)) { obj.put("loginError", CommonConfig.REQUEST_PARAMETERS_ARE_NULL); redirectAttrs.addFlashAttribute("state", obj); redirectAttrs.addFlashAttribute("user",requestParamUser); return "redirect:/"; } User user = userDAO.findByUserIdAndPassword(requestParamUser.getUserId(),requestParamUser.getPassword()); // Check provide userId and password is correct if (user == null) { obj.put("loginError", CommonConfig.USER_NOT_FOUND); redirectAttrs.addFlashAttribute("state", obj); redirectAttrs.addFlashAttribute("user",requestParamUser); return "redirect:/"; } obj.put("loginSuccess", CommonConfig.LOGIN_SUCCESS); redirectAttrs.addFlashAttribute("state", obj); session.setAttribute("userId",user.getUserId()); session.setAttribute("userName",user.getFirstName()); return "redirect:/"; } catch (Exception e) { System.out.println(CommonConfig.DB_ERROR + " : "+ e.getMessage().toString()); obj.put("loginError", CommonConfig.DB_ERROR); redirectAttrs.addFlashAttribute("state", obj); redirectAttrs.addFlashAttribute("user",requestParamUser); return "redirect:/"; } } This is the account method.
@RequestMapping(value="/ac") public String account(@RequestHeader(value = "Accept") String headerAccept,RedirectAttributes redirectAttrs, HttpServletRequest request){ String userId = (String) request.getSession(false).getAttribute("userId"); redirectAttrs.addFlashAttribute("abc",userId); return "account"; } in this method i'm trying to test the session veriable to redirectAttrs. That value will show in account view. Problem is in this method. I can not get the userId session veriable.