0

Hello I've got a problem with Session Attribute

I've 2 Controller Class

First Class (User)

@Controller
@RequestMapping("users")
@SessionAttributes("activeuser")
public class UserController {

    ....

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String  loginUser(HttpServletRequest request,Model model, @ModelAttribute("userlogininfo") User loginuser,
            BindingResult result, SessionStatus status) {
        User activeuser = userService.checkuserlogin(loginuser.getUsername(), loginuser.getPassword());

        if (activeuser!=null) {
            request.getSession().setAttribute("activeuser", activeuser);
            return "redirect:/home";
        } else
            return "redirect:/index";


    }

Second Class(bank)

@Controller
@RequestMapping("banka")
@SessionAttributes("activeuser")
public class BankController {
....
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String saveBanka(HttpServletRequest request, @ModelAttribute("banka") Banka banka, BindingResult result,
            SessionStatus status) {

        User user = (User) request.getSession().getAttribute("activeuser");

        banka.setUser(user);
        bankService.insert(banka);
        return "redirect:/home";
    }


}

Second Class is return null value from "(User) request.getSession().getAttribute("activeuser")"

Thanks in advance for your help.

2
  • 1
    Assuming if session is active when /save method is received by BankController, you should normally get the "activeuser" from session. To use the attributes defined in @SessionAttributes, try using public String saveBanka(...@ModelAttribute("activeuser") User user) and you should get the session attribute Commented Jul 13, 2017 at 20:55
  • Make sure your session object is Serializable. Commented Jul 13, 2017 at 21:24

1 Answer 1

1

Add your attribute to model (instead of request.getSession().setAttribute("activeuser", activeuser);)

model.addAttribute("activeuser", activeuser);

Make sure activeuser is Serializable.

Sign up to request clarification or add additional context in comments.

5 Comments

The OP is using session and not the request.
I believe the redirect: prefix will generate a 302 redirect http response and session will be created, so getting the session attribute into the redirected handler must get the session attribute.
@tsolakp He is obtaining session from request request.getSession().setAttribute("activeuser", activeuser); Request is dead after redirect, does not survive.
Does not matter different request can give the same session. That's the point of the session: to be the same in between requests (either with forward or redirect).
That would be true if he obtains it direct from HttpSession.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.