速度,不同的模板路径

编程入门 行业动态 更新时间:2024-10-22 15:38:21
本文介绍了速度,不同的模板路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有谁知道是否可以通过速度从不同路径获取模板?初始化后,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?

可能的计划流程:

  • 用户选择需要填入模板的组
  • 用户选择要使用的模板(可以位于硬盘驱动器的任何位置)
  • 用户按下生成
  • 推荐答案

    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");

    更多推荐

    速度,不同的模板路径

    本文发布于:2023-07-18 04:39:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1141096.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:路径   模板   速度

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!