1.添加依赖
<!--freemarker依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.添加配置文件
spring.freemarker.allow-request-override=false spring.freemarker.check-template-location=true spring.freemarker.charset=utf-8 spring.freemarker.content-type=text/html spring.freemarker.cache=true spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=false #spring.freemarker.prefix= spring.freemarker.suffix=.ftl #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= #spring.freemarker.template-loader-path=classpath:/template/ #comma-separated list #spring.freemarker.view-names=#whitelist of view names that can be resolved
3.添加页面helloFtl.ftl
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
Hello Freemarker ${name}
</body>
</html>
4.添加controller
package com.huawei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
/**
* 注意:
* 1.在Thymeleaf模板文件中,标签是需要闭合的,3.0之前是需要闭合的
* 2.thymeleaf3.0+是可以不强制要求闭合的。
*
* 3.同时支持使用多个模板引擎,比如thymeleaf和freemarker
*/
@Controller
@RequestMapping("/templates")
public class TemplatesController
{
@RequestMapping("/test")
public String hello(Map<String, Object> map) {
map.put("hello", "hello");
return "hello";
}
@RequestMapping("/helloFtl")
public String helloFtl(Map<String, Object> map) {
map.put("name", "wwww");
return "helloFtl";
}
}
运行结果:

本文详细介绍如何在Spring Boot项目中整合Freemarker模板引擎,包括添加依赖、配置参数、创建模板文件及控制器方法,实现动态页面渲染。
925

被折叠的 条评论
为什么被折叠?



