Grails get Session and Management in Service class
Asked Answered
T

4

17

I have a problem with Grails Session. I was thinking about having a Service Class for my session handling. So I created a class called "SessionService" (under grails-app/services/grails/).

class SessionService {
    static transactional = true
    GrailsWebRequest request = RequestContextHolder.currentRequestAttributes()
    GrailsHttpSession session = request.session

    def setTestvar(String value) {
        if (session != null)
            session.setAttribute("sTeststring", value)
    }

    def getTestvar() {
        if (session != null)
            session.getAttribute("sTeststring")
    }
}

The Problem is now, that I get a Nullpointer-Exception: "Method threw 'java.lang.NullPointerException' exception. Cannot evaluate org.codehaus.groovy.grails.web.servlet.mvc.GrailsHttpSession.ToString()".

Usage of my Service Class e.g. in a Controller:

class SampleController {

    SessionService sessionService

    def selectAnything = {

        sessionService.setTestvar("test-value")
        render(view: "testview")
    }
}

What am I'm doing wrong here? Is it the right way? Or do I have to set "session = request.session" in every method?

Hope to get help from you.

Thank you very much in advance.

Cheers,

Marco

Thornton answered 15/8, 2011 at 7:36 Comment(0)
H
31

You have to do something like

def session = RequestContextHolder.currentRequestAttributes().getSession()

in every method of your service. But it's not clear to me, why you need such a service. You can allways access the session in your Controller like this...

session.someAttribute = "someValue"

Christian

Hannahannah answered 15/8, 2011 at 10:36 Comment(7)
Hey Christian, thanks. But there's no "getSession()"-method available on currentRequestAttributes. Doing "Session manipulation(setting/getting)" directly can be error-prone, I want to avoid it an keep session variable naming and handling in one place ;-)Thornton
How did you check that getSession() ist not available? I tried the code in a grails console and it worked.Hannahannah
Oh I have to apologize, it works. BUT my IntelliJ underlines it that this method doesn't exist :-O Also in every documentation it's not available. Only OLD google results shows this solution. I'm not sure if this works in future?! I'm surprised :-O My solution was now: ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); return servletRequestAttributes.getRequest().getSession(true);Thornton
It's not in the interface, but it's in the Grails implementation class, so it's not clear to the IDE that it's available but it is, and is safe to use.Charlottetown
in new version of grails WebUtils has been implemented. see my answer #7063105Halda
Hi, where do you get RequestContextHolder from? Intellij cant find it, or where to import it from.Leach
@JohnLittle Hi John - I think something went wrong when you wrote the comment and it edited the actual answer!Niggle
C
19

Here is some sample code where I'm pulling session data and request data from a service without passing the request or session objects as a parameter to the service.

package beecomplete

import org.codehaus.groovy.grails.web.util.WebUtils

class HelperService {

    public User getCurrentUser() {
        def webUtils = WebUtils.retrieveGrailsWebRequest()
        return User.findById(webUtils.getSession().userid)
    }

    public Object getModelAttribute(String key) {

        def webUtils = WebUtils.retrieveGrailsWebRequest()
        return webUtils.getCurrentRequest().getAttribute(key)
    }
}
Carinthia answered 3/12, 2012 at 14:1 Comment(3)
WebUtils.retrieveGrailsWebRequest().session is a wonderfully elegant solution.Nepean
Great! How do you discover WebUtils class?Greg
import org.codehaus.groovy.grails.web.util.WebUtils not found for grails 3.3?Leach
H
11

For new versions (>2.2) of Grails:

import org.codehaus.groovy.grails.web.util.WebUtils

....
HttpServletRequest request = WebUtils.retrieveGrailsWebRequest().currentRequest
HttpSession session = request.session
Halda answered 14/10, 2014 at 13:18 Comment(0)
F
0

This also works

import grails.web.api.ServletAttributes

@Transactional
class AuthService implements ServletAttributes {

   // session will be available
Frodi answered 9/5, 2020 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.