Spring boot 整合 freemarker 模版引擎
- 引入jar包
//引入freemarker 模版引擎jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
- 编写控制器
@RequestMapping("/index")
@ResponseBody
public Map<String,Object> indeController(Map<String,Object> indexResPMap){
indexResPMap.put("name","LaiHeiHei");
indexResPMap.put("sex",1);
List<String> hobby = new ArrayList<>();
hobby.add("乒乓球");
hobby.add("跑步");
hobby.add("书法");
indexResPMap.put("hobby",hobby);
return indexResPMap;
}
- 插件freemarker配置文件
spring:
http:
encoding:
force: true
charset: UTF-8
freemarker:
allow-request-override: false
cache: false
check-template-location: true
content-type: text/html; charset=utf-8
charset: UTF-8
expose-request-attributes: false
expose-session-attributes: false
expose-spring-macro-helpers: false
##模版文件结尾
suffix: .ftl
##模版文件目录
template-loader-path: classpath:/templates
- 编写web页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>${name}</h1>
<p>${name} 是一个<#if sex == 1>男<#elseif sex==2 >女<#else >不知道呢</#if>孩子</p>
<p>爱好有:<#list hobby as like>
${like}
</#list></p>
</body>
</html>
网友评论