1. 程式人生 > >一個velocity的#set指令賦值null的坑

一個velocity的#set指令賦值null的坑

#set($price = $item.sku.price)
如果在一個兩次迴圈裡 item.sku.price第一次是5.0,第二次是null,那麼$price的值為兩個5.0,因為預設情況下給velocity變數使用#set指令賦值時,如果等式右邊為null,那麼不會解除等式左邊變數的引用。

If the RHS is a property or method reference that evaluates to null, it will not be assigned to the LHS. Depending on how Velocity is configured, it is usually not possible to remove an existing reference from the context via this mechanism. (Note that this can be permitted by changing one of the Velocity configuration properties).

解決辦法:
1. 不用#set指令,直接一串點點點(item.sku.price)輸出值
2. 在velocity配置檔案中配置 directive.set.null.allowed = true