2

这篇文章的简化问题是:

如何在 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 的文档。除了尝试打印到控制台之外,还有没有办法解决上下文为空的原因?

4

3 回答 3

1

我找到了答案,但我不太明白它为什么会起作用。我将上下文从 serverContextPath (返回 /)更改为 "" 并且它可以工作。

eventConfigureTomcat = {tomcat ->
    println "Changing the configuration for tomcat"
    println serverContextPath

    def ctx=tomcat.host.findChild(serverContextPath) // doesn't work?
    def ctx=tomcat.host.findChild("") // works
    ctx.allowLinking = true 
    println "Configuration changed"
} 
于 2012-09-27T11:11:54.897 回答
1

这是在 Grails 2.4.x 上对我有用的:

在 BuildConfig.groovy 中:

grails.appName = appName;

(不知道如何访问应用程序名称。您也可以以类似的方式添加您的 app.context)然后添加带有此内容的 scripts/_Events.groovy:

eventConfigureTomcat = { tomcat ->
    def ctx = tomcat.host.findChild("/${grailsSettings.config.grails.appName}")
    ctx.allowLinking = true
    println "Added symbolic link support for /${grailsSettings.config.grails.appName}"
}
于 2015-01-13T00:38:26.990 回答
0

没有一个答案对我很有效,但 Tobia 的评论帮助我正确设置了 ctx 变量。最后,我只需要像这样编辑 scripts/_Events.groovy:

eventConfigureTomcat = { tomcat ->
    def ctx = tomcat.host.findChildren()[0] 
    ctx.allowLinking = true
    println "Added symbolic link support for /${grailsSettings.config.grails.appName}"
}
于 2018-03-21T21:43:32.990 回答