Monday, September 5, 2011

Get HTTP session variables in Spring MVC controller

Normally I don't care to have Request or Session data "leaking" around my code, and limit it's awareness to the controller. But for this small project I'm porting from a desktop version of the software to a web-enabled one (Spring MVC), I didn't want too many layers of abstractions. In case I ever need to do this again:


ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
User loggedInUser = (User)attr.getAttribute("loggedInUser", RequestAttributes.SCOPE_SESSION);

No comments:

Post a Comment