- 概念
FreeMarker是一個模板引擎,一個基于模板生成文本輸出的通用工具,使用純Java編寫。
FreeMarker被設(shè)計用來生成HTML Web頁面,特別是基于MVC模式的應(yīng)用程序。
雖然FreeMarker具有一些編程的能力,但通常由Java程序準(zhǔn)備要顯示的數(shù)據(jù),由FreeMarker生成頁面,通過模板顯示準(zhǔn)備的數(shù)據(jù),簡單來講就是模板加數(shù)據(jù)模型,然后輸出頁面。
想學(xué)習(xí)更多請參考:https://freemarker.apache.org/
1.依賴jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.配置application.properties
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/view/
3.FreeMarker使用
- 后臺代碼
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import freemarker.template.Configuration;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequestMapping("marker/")
@Controller
public class FreeMarkerCtrl {
@Autowired
private Configuration configuration;
/**
* 實現(xiàn)方式1
* @param map
* @return
*/
@RequestMapping(value = "/users",method = RequestMethod.GET)
public String freeMarker(Map<String,Object> map){
map.put("users",parseUsers());
map.put("title","用戶列表");
return "users";
}
/**
* 實現(xiàn)方式2
* @param response
* @throws IOException
* @throws TemplateException
*/
@RequestMapping(value = "/users1",method = RequestMethod.GET)
public void freeMarker1(HttpServletResponse response) throws IOException, TemplateException {
Map<String,Object> map= new HashMap<>();
map.put("users",parseUsers());
map.put("title","用戶列表");
Template t = configuration.getTemplate("users.html");
String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, map);
System.out.println(content);
response.getOutputStream().write(content.getBytes("utf-8"));
}
private List<Map> parseUsers(){
List<Map> list= new ArrayList<>();
for(int i=0;i<10;i++){
Map map= new HashMap();
map.put("name","kevin_"+i);
map.put("age",10+i);
map.put("phone","1860291105"+i);
list.add(map);
}
return list;
}
}
- html
<html lang="zh-CN">
<head>
<meta charset="UTF-8"/>
<title>${title}</title>
<style>
table {
width: 50%;
font-size: .938em;
border-collapse: collapse;/*邊框合并*/
}
th {
text-align: left;
padding: .5em .5em;
font-weight: bold;
background: #66677c;color: #fff;
}
td {
padding: .5em .5em;
border-bottom: solid 1px #ccc;
}
table,table tr th, table tr td { border:1px solid #0094ff; }/*設(shè)置邊框*/
</style>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Phone</th>
</tr>
<#list users as user>
<tr>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.phone}</td>
</tr>
</#list>
</table>
</body>
</html>
運行結(jié)果:
20180607111214.png
更多語法請參考:https://blog.csdn.net/fhx007/article/details/7902040