Java Grails中的servletContext在哪里可用?

Java Grails中的servletContext在哪里可用?,java,grails,servlets,Java,Grails,Servlets,Grails应用程序中的servletContext在哪里可用?我试图在Config.groovy中访问它,但没有成功。在视图中访问它也是不可能的 我想使用storage=(File)servletContext[“javax.servlet.context.tempdir”]将temp目录设置为在我的开发和测试环境中存储上传文件的目录 按照Burt的建议,我在conf/Config.groovy中尝试了以下操作: environments { production {

Grails应用程序中的
servletContext
在哪里可用?我试图在
Config.groovy
中访问它,但没有成功。在视图中访问它也是不可能的

我想使用
storage=(File)servletContext[“javax.servlet.context.tempdir”]
将temp目录设置为在我的开发和测试环境中存储上传文件的目录

按照Burt的建议,我在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}"
    }

}
。。。但是得到这个错误:

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

您可以从任何控制器访问它。您可以从bootstrap.groovy访问它,但我对此表示怀疑。有关从控制器访问它的示例,请参阅

如果你的代码还没有访问权限,你可以使用
org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext()

我也有同样的问题。我在resource.groovy中需要它,但ServletContextHolder.getServletContext()返回null。grailsApplication.mainContext.servletContext也不起作用,因为mainContext还不存在。如果您更早地需要控制器,比如在Config中,或者在我的例子中,在resource.groovy中,控制器就太晚了。