First of all, @SessionAttribute
does not have to use the http session. It uses a SessionAttributeStore
which can have anything as its backing storage. Only the default implementation uses the http session.
The reason why your code does not work as expected lies in how @SessionAttribute
works.
Before a controller method is invoked, everything listed in @SessionAttributes
, in your case {"warenkorb", "count"}
, is read from the session and added to the model.
After the method has returned the session is updated with everything that has been added to the model within the method.
.addObject("count", count)
-> count is added to the model and afterwards to the session.
session.setAttribute("count", count)
-> count is added to the session but not to the model. It will be added to the model before the next invocation of any controller method. But as for now the model still has the old count
. And the model is what gets added to the request. And if an attribute can be found in the request scope then the jsp does not care about what's in the session.
When you use @SessionAttributes
and @ModelAttribute
(or Spring MVC in general) then avoid using HttpSession
or HttpRequest
. Even HttpResponse
is of limited use. Embrace the beauty of Spring MVC
instead :)