You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Thymeleaf配置UTF-8仍出现页面乱码问题求助

Spring Boot 国际化配置后页面乱码问题

我已编写如下MvcConfig配置类,同时在application.properties中配置spring.messages.encoding=UTF-8,Thymeleaf模板也设置了UTF-8编码,但浏览器显示仍存在乱码(见截图),请求协助解决。

MvcConfig配置类代码

@Configuration
@EnableWebSecurity
@EnableMethodSecurity(securedEnabled = true, jsr250Enabled = true)
public class MvcConfig implements WebMvcConfigurer {


    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.ENGLISH);
        return slr;
    }

    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @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

Thymeleaf模板代码片段

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      th:lang="${#locale.language}" lang="en">
<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
...
</html>

页面乱码截图

页面乱码截图


排查与解决建议

  • 检查国际化资源文件(如messages.properties、messages_zh_CN.properties等)的实际编码,确保文件本身以UTF-8格式保存。很多IDE默认编码可能是GBK,需手动在IDE设置中修改文件编码。
  • 增强ReloadableResourceBundleMessageSource配置,添加缓存控制和禁用系统本地编码回退:
    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setCacheSeconds(3600); // 设置缓存时间,避免旧编码缓存
        messageSource.setFallbackToSystemLocale(false); // 禁用系统本地编码回退
        return messageSource;
    }
    
  • 明确配置Thymeleaf视图解析器的编码,避免模板渲染时编码丢失:
    @Autowired
    private ApplicationContext applicationContext;
    
    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setCharacterEncoding("UTF-8");
        return templateResolver;
    }
    
    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }
    
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        registry.viewResolver(resolver);
    }
    
  • 配置全局Servlet编码,确保响应头携带正确编码:
    在application.properties中添加:
    server.servlet.encoding.enabled=true
    server.servlet.encoding.force=true
    server.servlet.encoding.charset=UTF-8
    
  • 检查浏览器响应头,通过开发者工具查看Content-Type是否包含charset=UTF-8,若未包含,说明后端未正确设置响应编码。

内容的提问来源于stack exchange,提问作者Nunyet Calçada

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 10:56:15