[英]Freemarker encoding wrong
我有項目是使用freemarker的碼頭服務器。 我在模板中使用俄語字母並收到?
。 我之前閱讀過的所有主題都對我沒有幫助。 我在我的代碼中使用
.setContentType("text/html;charset=utf-8");
.setCharacterEncoding("UTF-8");
我所有的文件都是utf-8。
我的Freemarker頁面生成器servlet
public class PageGenerator {
private static PageGenerator pageGenerator;
private Configuration cfg;
public static PageGenerator instance() {
if (pageGenerator == null)
pageGenerator = new PageGenerator();
return pageGenerator;
}
public String getPage(String filename, Map<String, Object> data) {
Writer stream = new StringWriter();
try {
Template template = cfg.getTemplate(filename);
template.process(data, stream);
} catch (IOException | TemplateException e) {
e.printStackTrace();
}
return stream.toString();
}
private PageGenerator() {
cfg = setCfg();
}
private Configuration setCfg(){
cfg = new Configuration(Configuration.VERSION_2_3_20);
try {
cfg.setDirectoryForTemplateLoading(new File("templates/"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
} catch (IOException e) {
e.printStackTrace();
}
return cfg;
}
}
但是在brouser中,我的頁面以Windows-1252編碼。
感謝您的想法ddekany
我使用了ddekany
模式,發現擴展HttpServletResponse的Class Response具有“魔術”方法.setCharacterEncoding()
,該方法在響應流中設置了字符編碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.