无法访问Grails 3中的Web应用程序文件夹文件

问题描述 投票:3回答:2

我无法访问Grails 3中的web-app文件夹文件。我在web-app文件夹中有robots.txt,在Grails 2中我可以直接在http://localhost:8080/robots.txt访问它。迁移到Grails 3后,我无法再访问http://localhost:8080/robots.txt了。

如何再次访问这些文件?

grails grails-3.0 grails-3.1
2个回答
6
投票

https://github.com/grails/grails-core/releases/tag/v3.0.12和部分Location of static resources

在Grails 3中,您可以将文件存储在src / main / resources下。您可以通过以static开头的文件名访问它们,例如http://localhost:8080/static/robots.txt

可以使用附加URL中定义的配置选项更改此路径


1
投票

我在Grails 3.3.1中遇到了类似的问题。我有必要访问文件模板以使用它与插件(excel-export插件)。阅读完文档后,我将文件放在src / main / resources下。它在开发模式(grails run-app)中使用OK,但我在生产环境中收到错误(grails war)。经过大量的阅读,我找到了让它发挥作用的方法。我将文件放在同一目录(src / main / resources)中,然后在我的控制器中:

def template = this.class.classLoader.getResource('myExcelFile.xlsx')
def path = template.file  //will give you the real path to your file

拥有该路径后,您可以打开流或执行您需要执行的操作。在我的情况下,使用插件:

new WebXlsxExporter(path).with {
  setResponseHeaders(response)
    add(products, withProperties)
    save(response.outputStream)
}
© www.soinside.com 2019 - 2024. All rights reserved.