Freemarker encoding - question marks in the place of accented characters

本秂侑毒 提交于 2019-12-01 02:33:49

问题


I am trying to print accented characters with Freemarker, but in the place of accented characters, I get only question marks. I have verified, that following statement holds:

 "UTF-8" == Environment.getCurrentEnvironment().getConfiguration().getDefaultEncoding()

I can easily see, that the accented characters are correctly held in the variable before giving it to the template.

My freemarker context can be found here: https://gist.github.com/1975239

For instance instead of:

 Jedinečný živý koncert, kde nejen, že uslyšíte, ale i uvidíte splynutí metalové kapely s padesátičlenným orchestrem včetně.

I keep getting:

 Jedine?ný ?ivý koncert, kde nejen, ?e usly?íte, ale i uvidíte splynutí metalové kapely s padesáti?lenným orchestrem v?etn?.

Thanks.


回答1:


FreeMarker always treats text as UNICODE, so it doesn't generate question marks. Since the accented letters aren't coming from the templates (if I understand it well), it must be your output encoding that's improper. See also: http://freemarker.org/docs/app_faq.html#faq_questionmark

BTW, getDefaultEncoding() has no role in this. That influences the decoding used when you load the templates, but you are saying that the accented characters aren't coming from the template file, also I don't think you can get ?-s from decoding (unless, for invalid UTF-8 byte sequences). As of the encoding of the output, FreeMarker just uses a Writer (as opposed to an OutputStream), so it can't influence that.




回答2:


I was able to resolve a similar issue with non-standard symbols (like ™) by setting the content-type on the FreeMarkerViewResolver:

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
...
    <property name="contentType" value="text/html;charset=UTF-8"/>
...
</bean>



回答3:


For DROPWIZARD Users: passing through the UTF-8 Charset in the constructor worked out:

import io.dropwizard.views.View;

import java.nio.charset.Charset;

public class SomeView extends View {
    public SomeView() {
        super("/views/some.ftl", Charset.forName("UTF-8"));
    }
}



回答4:


For the freemarker servlet there exist init parameters for encoding of template and output. You might compare it with your configuration.



来源:https://stackoverflow.com/questions/9559658/freemarker-encoding-question-marks-in-the-place-of-accented-characters

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