views:

24

answers:

1

I plan to create a Handler interceptor that will fire before the controller gets called (or I'll do this pre-action firing).

I will then check the user's cookie, and load the user object based on the sessionid in the cookie.

I will then add the user object to the request attributes.

Now if I want to retrieve the user object in my controllers action, do I cast it to (User) ?

I believe in my freemarker template I can just do ${user.name} correct? or is it user.getUsername ?

+2  A: 

First, you'd better place the user in the session, so that the cookie > user conversion does not happen on each request.

Second, you can just get it from there (session/request) by calling

User user = (User) session.getAttribute(USER_KEY); // this is s String constant

Alternatively, you can make a class UserHolder, where you pass an HttpSession and it gives you the user, thus sparing the casts in your controller cote.

The same approach can be used with HttpServletRequest.

Bozho
I dont' want to use sessions as this is for a distributed application and it can't rely on talking to the same server.
Blankman
you can, it's called session replication ;)
Bozho
i'd rather go the cookie with sessionID route, its more scalable and less error prone and doesnt' require JVM's to chat with each other.
Blankman
ok, as you wish. My answer holds for both session and request.
Bozho