【问题标题】:Velocity Unable To find Resources速度无法找到资源
【发布时间】:2017-06-09 17:53:49
【问题描述】:

这个问题已经被问过了,但我正在尝试接受答案中的相同内容

protected static final String RESOURCE_LOADER = classpath.resource.loader.class";    

static {
    System.out.println("Velocity Initialization Started");
    velocityEngine = new VelocityEngine();
    velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    velocityEngine.setProperty(RESOURCE_LOADER,ClasspathResourceLoader.class.getName());
    try {
        velocityEngine.init();
    } catch (Exception e) {

        LOG.error("Failed to load velocity templates e={}", e); 
    }
}

我的速度文件在

src/main/resources/velocity/templates/command/name.vm

我通过以下命令获取模板

template = velocityEngine.getTemplate("velocity/templates/command/GenericState.vm");

它在本地工作,但是当捆绑在 jar 中时它不起作用,我检查了它包含速度文件夹的 jar

我正在使用velocity生成java代码

我正在设置 maven 项目并且 maven 正在创建 jar

【问题讨论】:

    标签: apache maven velocity


    【解决方案1】:

    试试这个应该可以的。

    velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "class,file");
        velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
        velocityEngine.setProperty("runtime.log.logsystem.log4j.logger", "VELLOGGER");
        velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        velocityEngine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
        velocityEngine.init();
    

    【讨论】:

    • 并给出模板的完整路径,例如 template = velocityEngine.getTemplate("velocity/templates/command/GenericState.vm");
    • 您是否在您的代码中添加了我的代码?在速度和检查之前以这种方式尝试/velocityEngine.getTemplate("velocity/templates/command/Gener‌​icState.vm");../
    • 是的,它有效!但是后来我修改了之前的代码它也开始工作了,验证是什么导致了这个问题,接受这个解决方案,因为它也是正确的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2017-11-29
    • 2020-01-17
    • 2018-09-02
    相关资源
    最近更新 更多