raffian
raffian

Reputation: 32066

How to work with Grails Http sessions

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

Answers (4)

Ravindra Jha
Ravindra Jha

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

clever_bassi
clever_bassi

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

Dónal
Dónal

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:

Java

Object fooAttr = session.getAttribute("foo");

Grails

def fooAttr = session["foo"]

Upvotes: 4

bluesman
bluesman

Reputation: 2260

you can work with the "session" object

grails session

Upvotes: 3

Related Questions