Where is the SertextContext servlet available in Grails?

Where is the servletContext available in the Grails app? I tried to get it at Config.groovy , but that didn't work. Access to it in the view is also impossible.

I want to set the temp directory as the directory for storing downloaded files in my development and testing environment using storage = (File)servletContext["javax.servlet.context.tempdir"] .

Following Burt's advice, I tried the following in conf / Config.groovy:

 environments { production { grails.serverURL = "http://demo.com" } development { grails.serverURL = "http://localhost:8080/${appName}" storageDir = org.codehaus.groovy.grails.web.context.ServletContextHolder .getServletContext()["javax.servlet.context.tempdir"] } test { grails.serverURL = "http://localhost:8080/${appName}" } } 

... but get this error:

 java.lang.NullPointerException: Cannot get property 'javax.servlet.context.tempd ir' on null object at Config$_run_closure1_closure4.doCall(Config.groovy:59) at Config$_run_closure1_closure4.doCall(Config.groovy) at Config$_run_closure1.doCall(Config.groovy:57) at Config$_run_closure1.doCall(Config.groovy) at Config.run(Config.groovy:53) at _GrailsPackage_groovy$_run_closure1.doCall(_GrailsPackage_groovy:52) at _GrailsPackage_groovy$_run_closure2_closure10.doCall(_GrailsPackage_g roovy:93) at _GrailsPackage_groovy$_run_closure2_closure10.doCall(_GrailsPackage_g roovy) at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:2 80) at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy) at _GrailsPackage_groovy$_run_closure2.doCall(_GrailsPackage_groovy:92) at RunApp$_run_closure1.doCall(RunApp.groovy:28) at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381) at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415) at gant.Gant$_dispatch_closure7.doCall(Gant.groovy) at gant.Gant.withBuildListeners(Gant.groovy:427) at gant.Gant.this$2$withBuildListeners(Gant.groovy) at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source) at gant.Gant.dispatch(Gant.groovy:415) at gant.Gant.this$2$dispatch(Gant.groovy) at gant.Gant.invokeMethod(Gant.groovy) at gant.Gant.executeTargets(Gant.groovy:590) at gant.Gant.executeTargets(Gant.groovy:589) Failed to compile configuration file: Cannot get property 'javax.servlet.context .tempdir' on null object 
+4
source share
2 answers

You can use org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext() if you are in code that does not already have access to it.

+2
source

You can access it from any controller. You can access it from bootstrap.groovy, but I doubt it. For an example of accessing it from the controller, see http://www.grails.org/doc/latest/ref/Controllers/servletContext.html

+1
source

All Articles