First of all, @SessionAttribute should not use an http session. It uses a SessionAttributeStore , which can have anything as its backup storage. Only the default implementation uses an http session.
The reason your code is not working properly is because @SessionAttribute works.
Before calling the controller method, everything listed in @SessionAttributes , in your case {"warenkorb", "count"} , is read from the session and added to the model.
After the method returns , the session is updated with everything that was added to the model inside the method.
.addObject("count", count)
A counter is added to the model.
-> and then the session.
session.setAttribute("count", count)
-> count is added to the session, but not to the model. It will be added to the model until the next call to any controller method. But at the moment, the model still has an old count . And the model is added to the request. And if the attribute can be found in the request area, then jsp does not care about what is in the session.
When you use @SessionAttributes and @ModelAttribute (or Spring MVC in general), do not use HttpSession or HttpRequest . Even HttpResponse has limited use. Hug the beauty of Spring MVC instead :)
source share