我已经编写了非常基本的 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
指令本身初始化期间显示的,因此它不会由您正在执行的操作触发。您可以安全地忽略此警告,但如果您绝对不想再看到它,您可以:
- 将日志记录级别提高到
WARN
以上 - 编写您自己的自定义属性文件,或者只是从 Java 添加到您正在创建的
Properties
对象中,directive.foreach.counter.name
为空值,directive.foreach.iterator.name
和directive.foreach.counter.initial.value
。 - 或者,从类路径目录中的velocity jar 中提取
org/apache/velocity/runtime/defaults/velocity.properties
,并从中删除已弃用的设置。
关于java - Velocity 中已弃用的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13993632/