I have a question that will probably have a very simple solution. I’m running grails 1.3.7 and I’m trying to set a session variable like: session["username"] = uName where uName is a value returned from a database query.
The problem I’m having is that the keyword session seems to be not recognized by grails (it’s underlined). Further, when I actually try to run the app I get this error: No such property: session for class:.
I don’t have any imports, should I?
You only have the
sessionvariable available to you in controllers, taglibs and GSPs, not in services or domain classes. You can always access it via the thread-local holder, but bear in mind that you only have a session if the current thread is a request handler (i.e. not if it’s a background thread):but it’s generally better to keep logic that requires access to the HTTP request in controllers (or taglibs) where you know that you will always have a “current request”.