spring SpringBoot - UTF-8 在 messages.properties 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28112852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SpringBoot - UTF-8 Doesnt work in messages.properties
提问by QoP
?What's the problem?
?有什么问题?
I can't display in UTF-8 the messages I got in messages.properties.
我无法以 UTF-8 格式显示我在 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
我得到“Electr?nico”而不是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
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
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.
如果您需要有关我的配置的更多信息,我可以对其进行编辑。谢谢。
回答by Artem Bilan
To read any file in the UTF-8
encoding it must be created in the UTF-8
before.
要读取UTF-8
编码中的任何文件,它必须在UTF-8
之前创建。
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/
使用一些支持编码切换的编辑器。或者从 IDE 使用属性文件的编码选项创建该文件。例如想法:http: //blog.jetbrains.com/idea/2013/03/use-the-utf-8-luke-file-encodings-in-intellij-idea/
回答by ropo
回答by Paizo
Hopefully this may help others. The various code
solutions didn't work in my case but an editor settings did.
希望这可以帮助其他人。各种code
解决方案在我的情况下不起作用,但编辑器设置起作用了。
If you are using intellij go to file encoding
and set global and project encoding to UTF-8, same goes for the default encoding for properties files
and -very important- check the Transaparent native-to-ascii conversion
.
如果您正在使用 Intellij 转到file encoding
并将全局和项目编码设置为 UTF-8,那么同样适用default encoding for properties files
并且非常重要的检查Transaparent native-to-ascii conversion
.
If you open the file with a decent text editor you will notice that some characters are written with their UTF-8 version such as
如果您使用合适的文本编辑器打开文件,您会注意到某些字符是用它们的 UTF-8 版本编写的,例如
é
->
\u00E9
é
->
\u00E9
In my case the change didn't triggered a change for GIT so I had to make a "fake" modification to the file in order to do a commit and push.
在我的情况下,更改没有触发 GIT 的更改,因此我必须对文件进行“假”修改才能进行提交和推送。