Freemarker和Thymeleaf混用,springboot只装配了其中一个bean问题的解决

本文介绍了如何在SpringBoot项目中同时使用Freemarker和Thymeleaf,并解决只加载了一个模板引擎的问题。通过配置`application.properties`,设置Thymeleaf参数,然后创建自定义的`MvcConfig`配置类,包含`FreeMarkerViewResolver`,重写`loadView`方法以处理找不到模板文件的情况。在DispatcherServlet的视图解析器中,如果找不到视图,则会继续查找下一个解析器,实现了混合使用两个模板引擎。
摘要由CSDN通过智能技术生成

application.properties中,我只对 thymeleaf进行了配置 因为我发现freemarker的配置并没有被获取到。


spring.thymeleaf.encoding=UTF-8

spring.thymeleaf.cache=false

spring.thymeleaf.mode=HTML5

spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.checkTemplateLocation=false

spring.thymeleaf.suffix=.html 

 


接着创建一个配置类 MvcConfig 继承 WebMvcConfigurerAdapter:

 

 

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

@Configuration

@EnableAutoConfiguration

public class MvcConfig extends WebMvcConfigurerAdapter {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值