【问题标题】:Where do I store Excel Template resources for a Grails Application我在哪里存储 Grails 应用程序的 Excel 模板资源
【发布时间】:2014-08-21 04:08:52
【问题描述】:

我正在尝试通过来自控制器的字符串 URL 访问 Excel 模板。

  1. 我应该将我的 Excel 模板放在我的 Grails 项目中的哪个目录? (即 WEB-INF、grails-app/assets 等)
  2. 将其打包为 .war 并将其放入 Tomcat 7 容器中后的 URI 是什么??

编辑

我正在使用

def wb = new WebXlsxExporter("/path/to/excel/template")

这需要文件存储位置的字符串 URL。

【问题讨论】:

    标签: tomcat grails resources


    【解决方案1】:

    所以我找到了答案... ExcelTemplates 文件夹位于 web-app 文件夹中。为了在没有收到文件未找到错误的情况下获取 URL,我使用了

    request.getSession().getServletContext().getRealPath(.....)
    ---------
    //Which looks like this when implemented
    
    def wb = new WebXlsxExporter(request.getSession().getServletContext().getRealPath("/ExcelTemplates/testTemplate.xlsx"))
    

    当编译成war并放在Tomcat 7 /webapp目录下时,一切正常!

    【讨论】:

      【解决方案2】:

      您可以将它们放在 web-app 目录下。然后您可以使用以下代码作为 InputStream 来获取它们。

      private byte[] getLogoBytes() {
          InputStream logo = servletContext.getResourceAsStream('/images/logo.png')
          return logo.getBytes()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-18
        • 1970-01-01
        • 2014-01-30
        • 1970-01-01
        • 2015-02-21
        • 2011-02-15
        • 1970-01-01
        • 2020-10-04
        相关资源
        最近更新 更多