java 速度,不同的模板路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12669375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Velocity, different template paths
提问by Arninja
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".
有谁知道是否可以从不同路径以速度获取模板?初始化后,Velocity 拒绝更改“file.resource.loader.path”。
This is my code:
这是我的代码:
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));
}
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?
这是行不通的。似乎一旦 Velocity 被初始化,它就不能用不同的属性来重置。有关如何解决此问题的任何建议?
Possible Program flow:
可能的程序流程:
- User selects group that needs to be filled into the template
- User selects a template to use (can be located anywhere on the hdd)
- User presses generate
- 用户选择需要填入模板的组
- 用户选择要使用的模板(可以位于硬盘上的任何位置)
- 用户按下生成
回答by dogbane
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.
Velocity 可以通过两种方式使用:单例模型或单独的实例模型。您当前使用的是单例模型,其中只允许 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.
相反,您应该使用单独的实例模型,它允许您在同一个 JVM 中创建多个 Velocity 实例,以支持不同的模板目录。
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "path/to/templates");
ve.init();
Template t = ve.getTemplate("foo.vm");
回答by Vadzim
Consider instead of using singleton Velocity
class creating and initializing new VelocityEnginebefore step 3.
考虑在第 3 步之前使用单例Velocity
类创建和初始化新的VelocityEngine。
回答by devaj
Adding to the points above:
补充以上几点:
Even if one is using non-singleton model i.e using VelocityEngine
object. Multiple paths can be configured by giving comma separated values to the property.
即使使用非单例模型,即使用VelocityEngine
对象。可以通过为属性提供逗号分隔值来配置多个路径。
[file.resource.loader.class=path1,path2]
In such a case velocity engine will look for template in path1 first and then in path2
在这种情况下,速度引擎将首先在路径 1 中查找模板,然后在路径 2 中查找
回答by Gene Bo
In my case I am using Velocity with Servlets in an Eclipse Dynamic Web Project.
I couldn't actually reset the path, but I could put a subdirectory under /WebContent folder and then organize my templates that way... and have nested subdirectories as well.
就我而言,我在 Eclipse 动态 Web 项目中使用 Velocity 和 Servlet。
我实际上无法重置路径,但我可以在 /WebContent 文件夹下放置一个子目录,然后以这种方式组织我的模板......并且还有嵌套的子目录。
RequestDispatcher requestDispatcher =
request.getRequestDispatcher("/velocity_templates/index.vm");
This simple solution was all I needed ... didn't need to mess with velocity.properties in web.xml or setting them programmatically (in each case, neither approach worked for me unfortunately when I tried).
这个简单的解决方案就是我所需要的......不需要在 web.xml 中处理 velocity.properties 或以编程方式设置它们(在每种情况下,不幸的是,当我尝试时,这两种方法都不适合我)。
Note that when I do template includes with #parse(..) command, I need to use the same path prefix inside the template .vm file as I did in the example code for my servlet.
请注意,当我使用 #parse(..) 命令执行模板包含时,我需要在模板 .vm 文件中使用与我在 servlet 的示例代码中所做的相同的路径前缀。