1、在pom.xml中引入freemarker的jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2、在resources/application.yml配置文件中添加 freemarker的配置信息
spring: freemarker: allow-request-override: false cache: true charset: UTF-8 check-template-location: true content-type: text/html; suffix: .ftl template-loader-path: classpath:/templates/
3、创建PageController并添加index方法
package com.jeff.controller; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class PageController { @RequestMapping("index") public String index(Map<String, Object> map) { map.put("name", "Jeff"); map.put("sex", "0"); List<String> list = new ArrayList<>(); list.add("读书"); list.add("玩游戏"); list.add("敲代码"); map.put("hobbys", list); return "index"; } }
4、在src/main/resources下创建templates(默认访问此文件下的ftl),并创建index.ftl
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"/> <title></title> </head> <body> 姓名:${name}</br> 性别:<#if sex=="0">男<#elseif sex="1">女<#else>其它</#if></br> 爱好:<#list hobbys as hobby>${hobby} </#list> </body> </html>
5、打开浏览器访问index方法
来源:https://www.cnblogs.com/jiefu/p/10863039.html