我有一个SpringBoot应用。此胸腺叶模板:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
...
<li><label th:text="#{view.age}">
</label><span>25</span></li>
âge:
...
在属性文件上:
view.age=âge:
但是在浏览器上我看到了这个:
âge:25
âge:
在配置文件中:
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
在application.properties上:
spring.messages.encoding=UTF-8
spring.thymeleaf.encoding=UTF-8
IntelliJ IDEA也被设置为UTF-8
和
@Override
protected void configure(HttpSecurity http) throws Exception {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding("UTF-8");
encodingFilter.setForceEncoding(true);
http.addFilterBefore(encodingFilter, CsrfFilter.class);
..
}
我还试图将view.age=âge:
放入属性文件中,但随后在浏览器中看到了âge:
发布于 2020-12-21 03:09:09
我想你已经配置好了你几乎可以...只有几个额外的想法。
首先,请注意,只有当您在配置中do not define自己的MessageSource
,并且Spring Boot可以应用缺省消息源自动配置时,spring.messages.encoding
属性才会起作用。
如果你定义了你的MessageSource
,如果它应该有适当的编码(通过default,它是iso-8859-1
):
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource =
new ResourceBundleMessageSource();
messageSource.setBasename("messages"); // Please, set the right value
messageSource.setDefaultEncoding("UTF-8"); // Set the UTF-8 encoding
return messageSource;
}
您可以从不同的角度来看待它:如果您还没有这样做,也许在您的配置中包含一个像上面描述的自定义MessageSource
也可以解决这个问题。
此外,请确保属性文件采用所需的编码:如果您使用某种编码打开文件,Intellij很可能会保留该编码。尝试使用所需的编码UTF-8
重新加载文件。请注意,在许多情况下,这个过程是破坏性的,它会改变你的属性文件的内容,可能你需要编辑一些字符。当然,您可以使用适当的编码从头开始重新创建属性文件。
尽管我认为问题与属性文件的编码有关,但您也可以尝试设置为模板配置的ThymeleafViewResolver
的字符编码,并指明其字符编码和内容类型:
// In the way you have configured the view resolver for Thymeleaf
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
// the rest of your configuration
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setContentType("text/html; charset=UTF-8");
您可以尝试的最后一件事是在属性文件的内容中包含转义序列。
通常,当您的属性文件使用缺省编码iso-8859-1
,并且您需要使用JDK提供的工具native2ascii
时,就会执行此操作:
native2ascii [ inputfile ] [ outputfile ]
请注意,您可能需要两个文件,一个文件像往常一样使用特殊字符编写属性文件,另一个文件包含在对第一个文件应用native2ascii
命令后获得的转义序列。第二个文件是您需要在Spring中配置为i18n资源的文件。
按照同样的思路,如果您正在使用IntelliJ,您可以通过在属性文件的设置/首选项对话框的文件编码页面上选中相应的复选框来启用Transparent native-to-ascii conversion
。这将在幕后处理所提供的特殊字符和相应的转义序列之间的转换。请查看相关的documentation。
请注意,正如我之前告诉过您的,启用此特性将修改您的属性文件,因此在将更改提交到源代码控制时,请确保有一个备份副本或将其考虑在内。
发布于 2020-12-18 00:06:15
请尝试在您的application.properties中设置以下内容:
spring.messages.encoding=UTF-8
请让我知道这是否有用。
顺便说一句,关于类似的话题SpringBoot - UTF-8 Doesnt work in messages.properties,还有一个已经有几年历史的帖子了。
发布于 2020-12-19 23:08:21
模板编码将通过配置application.properties文件来工作-
spring.thymeleaf.encoding=UTF-8
https://stackoverflow.com/questions/65343072
复制相似问题