在使用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还在使用jetty吗?http://colinharrington.net/blog/2008/07/grails-jetty-and-crossdomainxml/
类似于question How to place certain files in the root directory of the Grails server?,但我需要一个包含在grails项目中的解决方案。
谢谢,
内森
发布于 2014-02-20 04:47:46
经过一些挖掘,这看起来很容易。
首先,嵌入式tomcat可以通过脚本/_Events.groovy和eventConfigureTomcat事件处理程序进行配置。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和/robots.txt启动grails将从脚本/rootContext/robots.txt获得服务。
https://stackoverflow.com/questions/21887689
复制相似问题