velocity:宏(#macro)出现中文乱码

来源:互联网 发布:中国云计算学术大会 编辑:程序博客网 时间:2024/06/07 02:18

最开始使用velocity时,输出中文乱码,我的解决办法 是在调用 mergeTemplate 方法时指定encoding为UTF-8,然后就正常了。

Velocity.mergeTemplate(String templateName, String encoding, Context context, Writer writer)

然而最近在使用velocity写模板时发现宏(#macro)中如果有中文,就会输出乱码(之前一直没有在macro中使用过中文)。宏之外的中文都能正常输出。
google搜索了一下,发现要彻底解决中文乱问题,还要设置input.encoding属性才行,如下:

Properties vprops = new Properties();vprops.put(Velocity.INPUT_ENCODING,"UTF-8");vprops.put(Velocity.OUTPUT_ENCODING,"UTF-8");// OUTPUT_ENCODING可以不设,因为我在调用mergeTemplate方法时已经指定了 encoding 为UTF-8Velocity.init(vprops);