FreeMarker Not Able Display Chinese Character

ε祈祈猫儿з 提交于 2019-12-13 06:43:48

问题


First time use FreeMarker on JAVA project and stack on configure the chinese character.

I tried a lot of examples to fix the code like below, but it still not able to make it.

// Free-marker configuration object              
Configuration conf = new Configuration();            
conf.setTemplateLoader(new ClassTemplateLoader(getClass(), "/"));   
conf.setLocale(Locale.CHINA);
conf.setDefaultEncoding("UTF-8");

// Load template from source folder              
Template template = conf.getTemplate(templatePath);                                 
template.setEncoding("UTF-8");

// Get Free-Marker output value              
Writer output = new StringWriter();              
template.process(input, output);

// Map Email Full Content            
EmailNotification email = new EmailNotification();  
email.setSubject(subject);              
.......

Saw some example request to make changes on the freemarker.properties but i have no this file. I just import the .jar file and use it.

Kindly advise what should i do to make it display chinese character.


回答1:


What exactly is the problem?

Anyway, cfg.setDefaultEncoding("UTF-8"); should be enough, assuming your template files are indeed in UTF-8. But, another place where you have to ensure proper encoding is when you convert the the template output back to "binary" from UNICODE text. So FreeMarker sends its output into a Writer, so everything is UNICODE so far, but then you will have an OutputStreamWriter or something like that, and that has to use charset (UTF-8 probably) that can encode Chinese characters.




回答2:


You need to change your file encoding of your .ftl template files by saving over them in your IDE or notepad, and changing the encoding in the save dialog. There should be an Encoding dropdown at the bottom of the save dialog.



来源:https://stackoverflow.com/questions/24306198/freemarker-not-able-display-chinese-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!