Grails get Session and Management in Service class

26,170

Solution 1

where does RequestContextHolder come from? Its not visible in grails 3.3.8 (in plugin at least)

Solution 2

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)
    }
}

Solution 3

For new versions (>2.2) of Grails:

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

....
HttpServletRequest request = WebUtils.retrieveGrailsWebRequest().currentRequest
HttpSession session = request.session
Share:
26,170

Related videos on Youtube

grailsInvas0r
Author by

grailsInvas0r

Updated on July 09, 2022

Comments

  • grailsInvas0r
    grailsInvas0r almost 2 years

    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

  • grailsInvas0r
    grailsInvas0r over 12 years
    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 ;-)
  • user852518
    user852518 over 12 years
    How did you check that getSession() ist not available? I tried the code in a grails console and it worked.
  • grailsInvas0r
    grailsInvas0r over 12 years
    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);
  • Burt Beckwith
    Burt Beckwith over 12 years
    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.
  • tzrlk
    tzrlk over 11 years
    WebUtils.retrieveGrailsWebRequest().session is a wonderfully elegant solution.
  • Mauro
    Mauro over 10 years
    Great! How do you discover WebUtils class?
  • demon101
    demon101 over 8 years
    in new version of grails WebUtils has been implemented. see my answer stackoverflow.com/questions/7062605/…
  • John Little
    John Little almost 5 years
    import org.codehaus.groovy.grails.web.util.WebUtils not found for grails 3.3?
  • John Little
    John Little almost 5 years
    Hi, where do you get RequestContextHolder from? Intellij cant find it, or where to import it from.
  • Don Cruickshank
    Don Cruickshank over 2 years
    @JohnLittle Hi John - I think something went wrong when you wrote the comment and it edited the actual answer!

Related