导入依赖
<!-- 添加freemarker模版的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
application.properties文件中新增freemarker配置
## Freemarker 配置 spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.cache=false spring.freemarker.charset=UTF-8 spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.request-context-attribute=request spring.freemarker.prefix=/ spring.freemarker.suffix=.ftl
在src/main/resource/templates文件夹中创建helloFtl.ftl文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index01</title> </head> <body> 欢迎: <#list listThree as three > ${three} </#list> </body> </html>
创建controller层
@Controller @RequestMapping("/free") public class FreeMarkerController {
@RequestMapping("/getThree") public String getThree(ModelMap map){ List<String> list=new ArrayList<>(); list.add("aa"); list.add("bb"); map.put("listThree",list); return "index"; } }
浏览器测试