当使用 run-app 运行 grails 应用程序时,如何从根上下文提供文件?例如,我想从 /robots.txt
提供 robots.txt 文件,并从 /mygrailsapp/
提供我的应用程序。
grails run-app 是否使用tomcat?如何配置 tomcat 插件以从根上下文提供静态文件?
到目前为止失败的路径...
1) 我找到了一种将项目设置为根上下文的方法,因此一切都从根上下文提供,但这不是我想要的。 http://nickcarroll.me/2009/03/27/configuring-the-grails-root-application-context/
2) 我发现了一篇旧文章 (2008),描述了如何使用 run-app 和 jetty 启用多个上下文。 run-app 是否仍然使用码头? http://colinharrington.net/blog/2008/07/grails-jetty-and-crossdomainxml/
类似于问题How to place certain files in the root directory of the Grails server?但我想要一个包含在 grails 项目中的解决方案。
谢谢,
内森
最佳答案
经过一些挖掘,看起来这很容易。
首先,嵌入式 tomcat 可以通过带有 eventConfigureTomcat 事件处理程序的 scripts/_Events.groovy 配置。 http://roshandawrani.wordpress.com/2011/03/13/grails-tip-configuring-embedded-tomcat-instance-used-in-developmenttest-env/
其次,您可以添加另一个上下文以从根目录提供文件。 http://grails.1312388.n4.nabble.com/Add-virtual-directory-in-grails-tomcat-plugin-td2234448.html#a2234448
import org.apache.catalina.loader.WebappLoader
eventConfigureTomcat = {tomcat ->
File dir = new File("./scripts/rootContext")
def rootContext = tomcat.addWebapp("/", dir.getAbsolutePath())
rootContext.reloadable = true
rootContext.loader = new WebappLoader(tomcat.class.classLoader)
//bonus answer - gzip json response
tomcat.connector.setAttribute("compression", "on")
tomcat.connector.setAttribute("compressableMimeType", "applicatio/json")
}
使用 run-app 启动 grails,/robots.txt
将从 scripts/rootContext/robots.txt 获得服务。
关于tomcat - grails run-app 从根上下文提供文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21887689/