Reputation: 32066
I'm new to Grails and I'm struggling with sessions..
In a controller, how do I check is the user has a valid session? In Java/Spring MVC, it's simple request.getSession(), so what's the convention in Grails?
How do I add/get values from the session?
Thanks,
Upvotes: 2
Views: 11129
Reputation: 108
session object in grails has two methods i.e-getAttribute() and setAttribute() which can be used to get/set value in session.
Upvotes: 0
Reputation: 2480
After the user logs in, set the value(username) in the session. For example, your domain class is User.groovy. So in your login method in controller:, you can do:
def login()
{
def u = new User()
u.properties[
'login',
'password',
] = params
if(u.save())
session.user = u
}
After that, whenever you want to check if the session is still on, just do
if(session.user != null)
//your code
Upvotes: 1
Reputation: 187529
Grails uses the same servlet API as Java EE, though it provides a few extra convenience methods. Within a Grails controller, taglib or GSP there are implicit variables request
and session
that refer to the current HTTPServletRequest and HttpSession.
Here's an example of how Grails makes it slightly more convenient to work with these objects:
Object fooAttr = session.getAttribute("foo");
def fooAttr = session["foo"]
Upvotes: 4