这篇文章的简化问题是:
如何在 Grails 中访问 WEB-APP 目录之外的文件?
我有一个 grails 应用程序,它与另一个系统有一些共享资源(图像)。所以我创建了一个指向新文件的符号链接。这些文件是由用户上传的,因此它们被有意放在 web-root 之外。
例如:
/images/country_flags/ --> /some/directory/with/images/country_flags/
所以当tomcat请求图片时:
/images/country_flags/flag1.png
它真的是:
/some/directory/with/images/country_flags/flag1.png
我知道 Tomcat 默认不支持符号链接,但可以通过在 META-INF 目录中创建一个 context.xml 文件来启用它,其中包含以下信息:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/images" allowLinking="true"></Context>
Grails 允许通过在脚本目录中创建一个名为“_Events.groovy”的新文件来配置 Tomcat。
从互联网上环顾四周,这应该可以解决问题:
eventConfigureTomcat = {tomcat ->
println "Changing the configuration for tomcat"
println serverContextPath
def ctx=tomcat.host.findChild(serverContextPath)
ctx.allowLinking = true
println "Configuration changed"
}
但是,我在控制台中得到以下输出:
Changing the configuration for tomcat
/
| Error Exception occurred trigger event [ConfigureTomcat]: Cannot set property 'allowLinking' on null object (Use --stacktrace to see the full trace)
我的application.properties文件如下(注意上下文改为“/”):
#Grails Metadata file
#Wed Sep 26 09:56:39 BST 2012
app.context=/
app.grails.version=2.1.1
app.name=SomeCoolApp
app.version=0.1
plugins.google-visualization=0.5.3
plugins.mail=1.0
plugins.quartz=1.0-RC2
plugins.searchable=0.6.3
plugins.spring-security-core=1.2.7.3
这些方法都不起作用。有人可以向我指出有关如何使用 _Events.groovy 方法配置 Tomcat 的文档。除了尝试打印到控制台之外,还有没有办法解决上下文为空的原因?