java - Velocity 中已弃用的属性

标签 java templates velocity

我已经编写了非常基本的 Velocity 模板,它使用 #foreach 指令。当我运行它时,我在日志中收到以下消息:

[warn] The directive.foreach.iterator.name property has been deprecated. It will be removed (along with $velocityHasNext itself ) in Velocity 2.0. Instead, please use $foreach.hasNext to access this value from now on.

但问题是我不使用此属性,至少没有明确地使用。我只是使用 #foreach 循环的基本形式,正如 user guide 中所示。 .

下面是产生此日志警告的简单代码。 Velocity版本为1.7。

final Properties properties = new Properties();
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init(properties);

StringWriter stringWriter = new StringWriter();
Map<String, Object> velocityVars = new HashMap<>();
List<Date> dates = new ArrayList<>(); 
dates.add(new Date()); 
dates.add(new Date());
velocityVars.put("dates", dates);
VelocityContext velocityContext = new VelocityContext(velocityVars);

String template =
                "foo\n" +
                "#foreach($d in $dates)\n" +
                "  $d\n" +
                "#end \n" +
                "bar";

velocityEngine.evaluate(velocityContext, stringWriter, "blah", template);
System.out.println(stringWriter.toString());

所以问题是 - 为什么会记录此警告以及如何构建我的代码以防止它出现?

最佳答案

directive.foreach.iterator.name 不是在代码中找到的内容,而是在 velocity.properties 文件中找到的内容。出于向后兼容性的原因,这个已弃用的定义仍然存在于默认配置中。

该警告是在 foreach 指令本身初始化期间显示的,因此它不会由您正在执行的操作触发。您可以安全地忽略此警告,但如果您绝对不想再看到它,您可以:

  1. 将日志记录级别提高到 WARN 以上
  2. 编写您自己的自定义属性文件,或者只是从 Java 添加到您正在创建的 Properties 对象中,directive.foreach.counter.name 为空值, directive.foreach.iterator.namedirective.foreach.counter.initial.value
  3. 或者,从类路径目录中的velocity jar 中提取org/apache/velocity/runtime/defaults/velocity.properties,并从中删除已弃用的设置。

关于java - Velocity 中已弃用的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13993632/

相关文章:

java - 如何使用 MockMvc 传递 ModelAttrubute 参数?

c++ - 如何使用模板查找最多的多个数字?

html - 你能为 "wrap"其他内容定义一个速度宏吗?

java - 如何使用最少的内存在哈希集中存储字符串

java - 合并两个 HashMap 的成本

java - 在 Eclipse 中替换库后未找到类异常

templates - 如何使用Beam 2.0创建Dataflow模板管道?

c++ - 使用模板创建默认函数参数

java - Velocity #foreach 不适用于动态值(范围)

java - 速度 - 更正正则表达式以删除控制字符?