SpringBoot集成thymeleaf(⾃定义)模板中⽂乱码的解决办 法
楼主今天在学习SpringBoot集成thymelaf的时候报了中⽂乱码的错误,经过⽹上的搜索,现在得到解决的办法,分享给⼤家:
package fig;
import org.springframework.beans.BeansException;
import org.t.properties.ConfigurationProperties;
import t.ApplicationContext;
import t.ApplicationContextAware;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.springframework.fig.annotation.ResourceHandlerRegistry;
import org.springframework.fig.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.plateresolver.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;
downlinker}
强力磁盘
我使⽤的是⾃定义的thymelefa模板,在配置⽂件中需要⼿动去配置上⾯的⼏个⽅法,这⾥给出thymeleaf部分配置⽂件和Controller类的截图代码:
大肠杆菌培养基
applicaiton.properties:
然后测试Controller:
下⾯是我的HTML代码:
启动项⽬之后:出现中⽂乱码烘干炉
解决办法如下图:(在最上⾯的配置⽂件相应位置加上下图红⾊箭头部分的代码)
重启项⽬即可解决中⽂乱码问题: