html - 属性文件中带有 Thymeleaf -UTF-8 的 Spring Boot CRUD 应用程序

标签 html spring spring-boot utf-8 thymeleaf

我有一个 SpringBoot 应用程序。这个 thymeleaf 模板:

        <!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=&acirc;ge:在属性文件中,但随后我看到 &acirc;ge:在浏览器中

最佳答案

我认为你几乎可以配置...只有几个额外的想法。
首先,请注意 spring.messages.encoding属性只有在您 do not define 时才有效您自己的 MessageSource在您的配置中,Spring Boot 可以应用默认消息源自动配置。
如果您定义了 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 时完成的。 ,并且您需要使用工具 native2ascii 随 JDK 提供:
native2ascii [ inputfile ] [ outputfile ]
请注意,您可能需要两个文件,一个像往常一样在其中编写带有特殊字符的属性文件,另一个带有应用 native2ascii 后获得的转义序列。命令第一个。第二个文件是您需要在 Spring 中配置为 i18n 资源的文件。
遵循相同的想法,如果您使用的是 IntelliJ,则可以启用 Transparent native-to-ascii conversion通过在属性文件的设置/首选项对话框的文件编码页面上选择相应的复选框。这将在后台处理提供的特殊字符和相应的转义序列之间的转换。请查看相关documentation .
请注意,正如我之前告诉您的,启用此特性会修改您的属性文件,因此请确保有一个备份副本,或者在您将更改提交到源代码管理时将其考虑在内。

关于html - 属性文件中带有 Thymeleaf -UTF-8 的 Spring Boot CRUD 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65343072/

相关文章:

javascript - 订购复选框及其标签

spring - 使用 Spring MVC 和 Hibernate 实现分页

java - 通过 Spring Web-Socket 定期向客户端发送消息

java - Spring Boot 中的验证嵌套模型

java - 如何在 spring boot 项目中忽略特定 URL 的 spring security CSRF

html - 在同一 ASP.NET MVC 表单中使用 2 个按钮

javascript - 如何使用 Canvas 绘制圆环图?

spring - 为什么Spring Boot会生成jar?

javascript - 将用户输入的日期转换为 JSON mm/dd/yyyy 格式并将其显示为结果

java - Redis List,弹出不移除