【问题标题】:Loading A STv4 Template From a Jar从 Jar 加载 STv4 模板
【发布时间】:2021-06-14 05:21:30
【问题描述】:

我在弄清楚如何迁移我的代码以使用 org.stringtemplate.v4 时遇到了一些麻烦。我在 src/main/resources 下的“模板”文件夹中有一个模板文件。没什么好看的。我只需要加载模板,设置一些属性,然后在我的 html 响应中传递它。问题是,模板被捆绑到部署的 jar 中,这意味着我无法进行正常的类路径查找。通常在这种情况下,我会使用 InputStream,但我找不到任何使用 ST 和 STGroup 的可用构造函数的方法。有谁知道如何做到这一点?有没有更简单的方法我只是忽略?

我的代码如下所示:

STGroup group = new STGroupDir("templates");
        ST template = group.getInstanceOf("smartSubmitResponse");
        template.add("errors", results.exceptions.asMap());
        template.add("plannedDates", results.plannedDates);
return Response.status(Response.Status.OK).header("smartSubmitFileUrl", url)
                .header("errors", errorHeaders.toString()).entity(template.toString()).build();

目前,由于模板设置不正确,因此失败并出现空指针异常。

【问题讨论】:

    标签: java maven wildfly stringtemplate-4


    【解决方案1】:

    我终于明白了。我需要使用 org.stringtemplate.v4.STRawGroupDir,因为我的模板使用的是没有换行符的旧格式。我的结果最终看起来像这样:

    STGroup group = new STRawGroupDir("templates",'$','$');
    ST template = group.getInstanceOf("smartSubmitResponse");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-21
      • 2011-02-25
      • 2013-01-23
      • 2011-05-11
      • 1970-01-01
      • 2013-10-12
      • 2017-04-20
      • 2017-01-23
      相关资源
      最近更新 更多