freemark介绍
FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。详细文档参考官方网址http://freemarker.foofun.cn/
主要文件结构
引入依赖
在pom.xml中引入freemarker依赖
<!-- 引入 freemarker 模板依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
添加配置信息
在application.properties中添加freemarker配置信息
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
# 关闭缓存, 即时刷新, 上线生产环境需要改为true
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=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
编写Controller
FreemarkerController.java
package com.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.pojo.Resource;
@Controller
@RequestMapping("ftl")
public class FreemarkerController {
@Autowired
private Resource resource;
@RequestMapping("/index")
public String index(ModelMap map) {
map.addAttribute("resource", resource);//在模板中添加要展示的数据
return "freemarker/index"; //返回的是文件路径
}
@RequestMapping("center")
public String center() {
return "freemarker/center/center";
}
}
Resource.java
package com.example.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix = "com.demo.opensource")
@PropertySource(value = "classpath:resource.properties")
public class Resource {
private String name;
private String website;
private String language;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
@Override
public String toString() {
return "Resource [name=" + name + ", website=" + website + ", language=" + language + "]";
}
}
Resouce.java引入的配置文件resouce.properties
com.demo.opensource.name=demo
com.demo.opensource.website=www.demo.com
com.demo.opensource.language=java
前端模板文件
center.ftl
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
FreeMarker模板引擎
<h1>center page</h1>
</body>
</html>
index.ftl
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
FreeMarker模板引擎
<h1>${resource.name}</h1>
<h1>${resource.website}</h1>
<h1>${resource.language}</h1>
</body>
</html>
运行效果
访问http://localhost:8088/ftl/index
访问http://localhost:8088/ftl/center