SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法

摘要:
楼主今天在学习SpringBoot集成thymelaf的时候报了中文乱码的错误,经过网上的搜索,现在得到解决的办法,分享给大家:packagecom.imooc.config;importorg.springframework.beans.BeansException;importorg.springframework.boot.context.properties.ConfigurationPr

楼主今天在学习SpringBoot集成thymelaf的时候报了中文乱码的错误,经过网上的搜索,现在得到解决的办法,分享给大家:

package com.imooc.config;

import org.springframework.beans.BeansException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

/**
 * WebMvc的配置类(自定义Thymeleaf模板)
 *
 * @author Liao Huan
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    /**
     * 设置上下文
     *
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * Thymeleaf模板资源解析器(自定义的需要做前缀绑定)
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.thymeleaf")
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();

        templateResolver.setApplicationContext(this.applicationContext);
        return templateResolver;
    }

    /**
     * Thymeleaf标准方言解释器
     */
    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        //支持spring EL表达式
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    /**
     * 视图解析器
     */
    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        thymeleafViewResolver.setTemplateEngine(templateEngine());

        return thymeleafViewResolver;
    }

我使用的是自定义的thymelefa模板,在配置文件中需要手动去配置上面的几个方法,这里给出thymeleaf部分配置文件和Controller类的截图代码:

applicaiton.properties:

SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法第1张

然后测试Controller:

SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法第2张

下面是我的HTML代码:

SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法第3张

启动项目之后:出现中文乱码

SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法第4张

解决办法如下图:(在最上面的配置文件相应位置加上下图红色箭头部分的代码)

SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法第5张

SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法第6张

重启项目即可解决中文乱码问题:

SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法第7张

原文地址:https://blog.csdn.net/qq_32575047/article/details/82927873

免责声明:文章转载自《SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇分类任务评价——混淆矩阵、精度、召回率的具体解释MySQL 数据库输入密码后闪退的问题及解决办法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringBoot thymeleaf模板版本,thymeleaf模板更换版本

SpringBoot thymeleaf模板版本 thymeleaf模板更换版本 修改thymeleaf模板版本 ================================ ©Copyright 蕃薯耀 2018年3月27日 http://www.cnblogs.com/fanshuyao/ 一、SpringBoot 使用thymeleaf模板需要引...

sprignboot 中thymeleaf和freemarker 都存在时,默认选择哪个

我们  无聊的时候想到,freemarker和thymeleaf都是springboot默认支持的模板,当这2个同时存在并有相同名字的时候,springboot会默认选择哪个模板来显示呢 ? 所以今天我就实际操作一下 ,都用默认的,不采用其他的配置来观察一下 ,。导入这2个模板的jar <dependency> <grou...

SpringBoot-Thymeleaf模板引擎整合及基本用法总结

兴趣的朋友可以去了解一下前四篇,你的赞就是对我最大的支持,感谢大家! (一) SpringBoot起飞之路-HelloWorld (二) SpringBoot起飞之路-入门原理分析 (三) SpringBoot起飞之路-YAML配置小结(入门必知必会) (四) SpringBoot起飞之路-静态资源处理 说明: 太忙啦,同时全放到一起,后来感觉移动端篇...

SpringBoot怎么访问html文件

pom.xml    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId&...

SpringBoot+Thyemleaf开发环境正常,打包jar发到服务器就报错Template might not exist or might not be accessible

网上查看了各种解决的思路,总结如下: 1. 在controller层请求处理完了返回时,没有使用@RestController或@ResponseBody而返回了非json格式 这种情况下返回的数据thymeleaf模板无法解析,直接报错,本人正式因为这个原因才报错。 解决方案:可以将@Controller换成@RestController,不过需要注意...

CAS单点登录-自定义登录页、修改编辑登录页

一、前言 我们搭建了CAS-Server,在浏览器中输入http://ip:port/cas/login之后,跳转到了统一认证中心的登陆界面。 如下图所示: 但是这个页面的logo和布局都是默认apereo的,我们希望自己编辑页面布局。 自己去定义布局,比如完成如下图: 自定义登陆界面,我们需要修改的地方包括这几点: 1.修改浏览器标签页图标ic...