1. 添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2. 配置yml:
spring: freemarker: template-loader-path: classpath:/templates/ #模板加载路径 charset: UTF-8 #编码格式
3. 读取freemarker处理结果的公共类:
import freemarker.template.Template; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import java.io.StringWriter; import java.util.Map; @Configuration @Slf4j public class FreeMarkerConfig { @Autowired private FreeMarkerConfigurer configurer; public St