1.什么是freemaker?
FreeMarker 是一款模板引擎:即一种基于模板和要改变的数据,并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
FreeMarker原理
FreeMarker是一个基 于Java的开发包和类库的一种将模板和数据进行整合并输出文本的通用工具,FreeMarker实现页面静态化的原理是:将页面中所需要的样式写入到 FreeMarker模板文件中,然后将页面所需要的数据进行动态绑定并放入到Map中,然后通过FreeMarker的模板解析类process()方 法完成静态页面的生成。其工作原理如图2-1所示。
模板 + 数据模型 = 输出
2.代码工程
实验目标:实现freemaker展现后台变量
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>freemaker</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
</project>
config
设置一个共享变量app
package com.et.freemaker.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import freemarker.template.TemplateModelException;
import javax.annotation.PostConstruct;
@Configuration
public class FreemarkerConfiguration {
//Configuration
@Autowired
private freemarker.template.Configuration configuration;
@PostConstruct
public void configuration() throws TemplateModelException {
// add globe variable
this.configuration.setSharedVariable("app", "Spring Boot");
}
}
controller
设置一个title变量
package com.et.freemaker.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public Map<String, Object> showHelloWorld(){
Map<String, Object> map = new HashMap<>();
map.put("msg", "HelloWorld");
return map;
}
@GetMapping("/")
public ModelAndView index () {
ModelAndView modelAndView = new ModelAndView("index/index");
// add title to Model
modelAndView.addObject("title", "Freemarker");
return modelAndView;
}
}
模版
放在resource/templates/index下面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Freemarker</title>
</head>
<body>
Hello ${title}!${app}
</body>
</html>
application.yaml
server:
port: 8088
spring:
freemarker:
enabled: true
cache: false
# Content Type
content-type: text/html
charset: utf-8
suffix: .ftl
request-context-attribute: request
expose-request-attributes: false
expose-session-attributes: false
allow-request-override: true
allow-session-override: true
expose-spring-macro-helpers: true
check-template-location: true
prefer-file-system-access: true
template-loader-path:
- classpath:/templates/
settings:
datetime_format: yyyy-MM-dd HH:mm:ss
template_update_delay: 30m
default_encoding: utf-8
以上只是一些关键代码,所有代码请参见下面代码仓库
代码仓库
- GitHub - Harries/springboot-demo: a simple springboot demo with some components for example: redis,solr,rockmq and so on.
3.测试
启动springboot应用
访问首页
http://127.0.0.1:8088/,返回内容
Hello Freemarker!Spring Boot
4.引用
- FreeMarker :: Spring Framework
- Spring Boot集成freemaker快速入门demo | Harries Blog™