views:

4143

answers:

1

What's the best way of storing session related data of a user (like, for example a log of recent actions a user has done) in a Spring MVC (2.5) web application ?

Using the classic javax.servlet.http.HttpSession or by specifying scope="session" in controller beans, and storing the data in a session object ?

+6  A: 

Session-scoped beans (using scope="session") is the cleanest approach. This removes the need to interact with the session yourself.

If you want to autowire a session-scoped bean in to the controller, you either need to make the controller session-scoped itself, or use a scoped-proxy to wire it into a singleton controller, as described here. Either approach is valid.

skaffman
thank you, a cleaner approach was exactly what i was searching for
dakull
Thankyou for this excellent answer, I had been using the HttpSession directly up until now.Making the Controller Request scoped (ie the old Throwaway Controller) also works and avoids the need for the scoped-proxy. Are there any downsides to this?
Dick Chesterwood
@Dick: request-scoped beans bring a performance penalty, but as long as your controller doesn't have an expensive custom init process, or your traffic isn't too high, it shouldn't be significant.
skaffman
Excellent, thanks skaffman confirmed what I thought.
Dick Chesterwood