mindas
mindas

Reputation: 26703

Deprecated property in Velocity

I have written pretty basic Velocity template which uses #foreach directive. When I run it, I get the following message in the log:

[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.

But the problem is that I don't use this property, at least explicitly. I just use #foreach loop in its basic form, exactly as shown in the user guide.

Below is the simple code which yields this log warning. Velocity version is 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());

So the question is - why is this warning logged and how do I structure my code to prevent it from appearing?

Upvotes: 2

Views: 1141

Answers (1)

Sergiu Dumitriu
Sergiu Dumitriu

Reputation: 11601

directive.foreach.iterator.name is not something found in code, but in a velocity.properties file. And for backwards compatibility reasons, this deprecated definition is still present in the default configuration.

The warning is displayed during the initialization of the foreach directive itself, so it's not triggered by something you're doing. You can safely ignore this warning, but if you definitely don't want to see it anymore, you could:

  1. Raise the logging level above WARN
  2. Write you own custom properties file, or just add from Java in the Properties object that you're creating, empty values for directive.foreach.counter.name, directive.foreach.iterator.name and directive.foreach.counter.initial.value.
  3. Or, extract org/apache/velocity/runtime/defaults/velocity.properties from the velocity jar in your classpath directory, and remove from it the deprecated settings.

Upvotes: 2

Related Questions