速度,不同的模板路径 [英] Velocity, different template paths

查看:105
本文介绍了速度,不同的模板路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道是否可以通过速度从不同路径获取模板?初始化后,Velocity拒绝更改file.resource.loader.path。

Does anyone know if it is possible to get templates from different paths with velocity? After initialization Velocity refuses to change the "file.resource.loader.path".

这是我的代码:

public Generator(){         
    Properties p = new Properties();
        p.setProperty("resource.loader", "file");
        p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
        p.setProperty("file.resource.loader.path", "");

    Velocity.init(p);
}

模板可以位于不同的位置(用户可以选择一个文件对话框)。所以我在从速度中取出模板时有这个代码

The templates can be located in different locations ( the user can select one with a file dialog ). So I have this code upon fetching the template out of velocity

private Template fetch (String templatePath) {
    out_println("Initializing Velocity core...");
    int end = templatePath.lastIndexOf(File.separator); 

    Properties p = new Properties();
        p.setProperty("file.resource.loader.path", templatePath.substring(0, end));
    Velocity.init(p);

    return Velocity.getTemplate(templatePath.substring(end+1));
}

这不起作用。似乎一旦Velocity初始化,它就不能用不同的属性重置。有关如何解决此问题的任何建议?

This is not working. It seems that once Velocity is initialized it can't be reset with different properties. Any suggestions on how to solve this problem?

可能的计划流程:


  1. 用户选择需要填入模板的组

  2. 用户选择要使用的模板(可以位于硬盘驱动器的任何位置)

  3. 用户按下生成


推荐答案

Velocity可以通过两种方式使用: 单身模型或单独的实例模型。您当前正在使用单例模型,其中只允许JVM中的一个Velocity引擎实例。

Velocity can be used in two ways: the singleton model or the separate instance model. You are currently using the singleton model in which only one instance of the Velocity engine in the JVM is allowed.

相反,您应该使用单独的实例模型,它允许您在同一个JVM中创建多个Velocity实例,以支持不同的模板目录。

Instead, you should use the separate instance model which allows you to create multiple instances of Velocity in the same JVM in order to support different template directories.

VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "path/to/templates");
ve.init();
Template t = ve.getTemplate("foo.vm");

这篇关于速度,不同的模板路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆