Velocity Unable to find resource

在maven的velocity项目中将resources下的模板资源文件打包。

将velocity模块化引入到其它项目时报Unable to find resource '';Velocity.getTemplate获取资源文件出错!

VelocityContext velocityContext = new VelocityContext(context.toMap());
Template template = Velocity.getTemplate("mybatis\\Model.java.vm", "UTF-8")
try (StringWriter writer = new StringWriter()) {
    template.merge(velocityContext, writer);
    return writer.toString();
} catch (IOException e) {
    e.printStackTrace();
}

解决方案1:

配置maven的build(可选择其它方式配置)

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

即可。

解决方案2:

使用变量编译模板。

final String templateContent = "package com.sicex.db.entity.${table.classPackage};
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Velocity中,我们可以通过设置 `resource.loader` 属性来指定模板路径。如果想要指定静态模板路径,可以使用 `org.apache.velocity.runtime.resource.loader.FileResourceLoader` 类,该类可以从文件系统中加载模板。 以下是一个示例配置文件,展示了如何指定静态模板路径: ``` resource.loader = file file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader file.resource.loader.path = /path/to/templates file.resource.loader.cache = true file.resource.loader.modificationCheckInterval = 2 ``` 在上面的示例中,`resource.loader` 属性被设置为 `file`,表示我们使用文件系统作为模板加载器。`file.resource.loader.class` 属性被设置为 `org.apache.velocity.runtime.resource.loader.FileResourceLoader`,表示我们使用该类来加载模板。`file.resource.loader.path` 属性被设置为静态模板路径,例如 `/path/to/templates`。`file.resource.loader.cache` 属性表示是否缓存模板,`file.resource.loader.modificationCheckInterval` 属性表示检查模板文件修改的时间间隔(以秒为单位)。 通过以上配置,当使用Velocity引擎来渲染模板时,它会从指定的静态模板路径加载模板文件。例如,如果我们有一个名为 `mytemplate.vm` 的模板文件,它位于 `/path/to/templates` 目录中,可以使用以下代码来渲染它: ``` VelocityEngine engine = new VelocityEngine(); engine.init(); Template template = engine.getTemplate("mytemplate.vm"); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值