2012-12-21 52 views
2

我寫了使用#foreach指令的相當基本的Velocity模板。當我運行它,我在日誌中得到以下信息:Velocity中不推薦使用的屬性

[提醒]在directive.foreach.iterator.name物業已經 棄用。它將在Velocity 2.0中被刪除(以及$ velocityHasNext本身) 。相反,請使用$ foreach.hasNext從現在開始訪問此 值。

但問題是,我至少明確地不使用此屬性。我只是使用#foreach循環的基本形式,完全如user guide所示。

下面是產生此日誌警告的簡單代碼。速度版本是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()); 

所以,問題是 - 這是爲什麼警告記錄以及如何構建我的代碼,以防止它出現?

回答

2

directive.foreach.iterator.name在代碼中找不到,但是在velocity.properties文件中。出於向後兼容的原因,這個不推薦的定義仍然存在於默認配置中。

警告在foreach指令本身的初始化期間顯示,因此它不是由您正在執行的操作觸發的。您可以放心地忽略此警告,但如果你絕對不希望再看到它,你可以:

  1. 提高上述WARN
  2. 寫日誌記錄級別您自己的自定義屬性文件,或者只是從Java加在您創建的Properties對象中,空值爲directive.foreach.counter.namedirective.foreach.iterator.namedirective.foreach.counter.initial.value
  3. 或者,從classpath目錄下的velocity jar中提取org/apache/velocity/runtime/defaults/velocity.properties,並從中刪除已棄用的設置。
+0

謝謝!順便說一句,#2的建議不起作用。爲這些屬性指定空字符串會給出相同的警告。看起來處理默認值並不是Velocity最強大的一面。 – mindas

相關問題