2010-11-24 17 views
4

Wo ist die in einer Grails-Anwendung verfügbar? Ich habe versucht, es in der Config.groovy zu erreichen, aber es hat nicht funktioniert. Der Zugriff in einer Ansicht ist ebenfalls nicht möglich.Wo ist der ServletContext in Grails verfügbar?

Ich möchte das temporäre Verzeichnis als das Verzeichnis zum Speichern von hochgeladenen Dateien in meiner Entwicklungs- und Testumgebung mit storage = (File)servletContext["javax.servlet.context.tempdir"] festlegen.

Burts Beratung Nach versuchte ich folgend 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}" 
    } 

} 

... aber diesen Fehler:

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 

Antwort

1

Sie es von jedem Controler zugreifen können. Sie können möglicherweise von bootstrap.groovy darauf zugreifen, aber ich bezweifle es. Ein Beispiel für den Zugriff auf einen Controller finden Sie unter http://www.grails.org/doc/latest/ref/Controllers/servletContext.html

+0

Ein Controller ist viel zu spät, wenn Sie es früher brauchen, wie in Config, oder in meinem Fall in resource.groovy. – mcv

2

Sie können org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext() verwenden, wenn Sie Code haben, der noch nicht darauf zugreifen kann.

+0

Ich habe es versucht, aber es funktioniert nicht. Siehe Beispiel oben. – deamon

+0

Ich habe das gleiche Problem. Ich brauche es in resource.groovy, aber ServletContextHolder.getServletContext() gibt null zurück. grailsApplication.mainContext.servletContext funktioniert auch nicht, da mainContext noch nicht existiert. – mcv

Verwandte Themen