Freemarker encoding - question marks instead of accented characters

I'm trying to print accented characters with Freemarker, but instead of accented characters, I get only question marks. I verified that the following statement holds:

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

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

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

For example, 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.

+4
source share
4 answers

FreeMarker always treats text as UNICODE, so it does not generate question marks. Since letters with an accent do not come from patterns (if I understand this well), this should be incorrect output coding. See Also: http://freemarker.org/docs/app_faq.html#faq_questionmark

BTW, getDefaultEncoding() does not play any role in this. This affects the decoding used when loading templates, but you say that the accented characters do not come from the template file, also I don’t think you can get it ? -s from decoding (if only for invalid UTF -8 byte sequences). Starting with encoding output, FreeMarker just uses Writer (unlike OutputStream ), so it cannot influence this.

+1
source

I managed to solve a similar problem with non-standard characters (e.g. ™) by setting the content type to FreeMarkerViewResolver:

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

For DROPWIZARD users: passing in UTF-8 encoding in the constructor:

 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")); } } 
+5
source

There are init parameters for the freemarker servlet to encode the template and output. You can compare it with your configuration.

0
source

Source: https://habr.com/ru/post/1399690/


All Articles