Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot - UTF-8 Doesnt work in messages.properties

Tags:

¿What's the problem?

I can't display in UTF-8 the messages I got in messages.properties.

An example

<h1 id="logo">Electrónico</h1> 

this works okay but when I try to use my message source like this

<h1 id="logo" th:text="#{titulo.electronico}">Electrónico</h1> 

I get "Electr�nico" instead of Electrónico

This is my configuration

application.properties

spring.messages.encoding=UTF-8 server.tomcat.uri-encoding=UTF-8 spring.http.encoding.charset=UTF-8 spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 

pom.xml

<properties>     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>     <start-class>demo.Demo</start-class>     <java.version>1.7</java.version> </properties> 

Demo class

@SpringBootApplication public class Demo {      public static void main(String[] args) {         SpringApplication.run(Demo.class, args);     } } 

ServletInitializer.class

@Configuration public class ServletInitializer extends SpringBootServletInitializer {      @Override     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {         return application.sources(Demo.class);     }      @Bean     public ServletRegistrationBean h2servletRegistration() {         ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());         registration.addUrlMappings("/console/*");         return registration;     }      @Bean     @Order(Ordered.HIGHEST_PRECEDENCE)     CharacterEncodingFilter characterEncodingFilter() {       CharacterEncodingFilter filter = new CharacterEncodingFilter();       filter.setEncoding("UTF-8");       filter.setForceEncoding(true);       return filter;     } } 

If you need more information about my configuration I can edit it. Thanks.

like image 945
QoP Avatar asked Jan 23 '15 15:01

QoP


2 Answers

To read any file in the UTF-8 encoding it must be created in the UTF-8 before.

Use some editor which supports encoding switching. Or create that file from IDE with encoding option for properties files. E.g. IDEA: http://blog.jetbrains.com/idea/2013/03/use-the-utf-8-luke-file-encodings-in-intellij-idea/

like image 57
Artem Bilan Avatar answered Oct 02 '22 04:10

Artem Bilan


In my case, I forgot messageSource.setDefaultEncoding("UTF-8"), see here.

like image 26
ropo Avatar answered Oct 02 '22 02:10

ropo