Grails get Session and Management in Service class
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
Related videos on Youtube
grailsInvas0r
Updated on July 09, 2022Comments
-
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 over 12 yearsHey 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 over 12 yearsHow did you check that getSession() ist not available? I tried the code in a grails console and it worked.
-
grailsInvas0r over 12 yearsOh 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 over 12 yearsIt'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 over 11 years
WebUtils.retrieveGrailsWebRequest().session
is a wonderfully elegant solution. -
Mauro over 10 yearsGreat! How do you discover WebUtils class?
-
demon101 over 8 yearsin new version of grails WebUtils has been implemented. see my answer stackoverflow.com/questions/7062605/…
-
John Little almost 5 yearsimport org.codehaus.groovy.grails.web.util.WebUtils not found for grails 3.3?
-
John Little almost 5 yearsHi, where do you get RequestContextHolder from? Intellij cant find it, or where to import it from.
-
Don Cruickshank over 2 years@JohnLittle Hi John - I think something went wrong when you wrote the comment and it edited the actual answer!