本文IDE:idea
环境:maven+Springboot+Freemarker
文件结构:
本文采取的测试方法有【1、固定值;2、循环生成固定值;3、读取配置文件;4、读取数据库】
1、pom.xml 文件导包
<!--这个包是服务于@ConfigurationProperties注解的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--Freemarker插件包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
2、创建Controller
package com.uflinux.demo.controller;
import com.uflinux.demo.util.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @program: springbootdemo
* @description:第一个Freemarker的Demo,获取静态值返回并生成页面
* @author: Feng_Lei
* @create: 2020-04-15 17:47
**/
@Controller
public class FreeMarkerCtrl {
/**
* @Description: 一、此处是第一个Freemarker的例子
* @Param: [表达意思为返回一个model,key=name,并获取写死的那么的值,最后返回到demo。html页面]
* @return: java.lang.String
* @Author: Mr.Feng
* @Date: 2020/4/15
*/
@RequestMapping(value = "index")
public String index(Model map){
map.addAttribute("title","这是一个静态表格的ftl");
map.addAttribute("name","风斯托洛夫斯基");
map.addAttribute("age","30+");
map.addAttribute("phone","110");
return "freemarker/center";
}
/**
* @Description:下面的这个方法预想是得到一个类的值,通过动态生成数据
* @Param:
* @return:返回的是动态生成的列表
* @Author: Mr.Feng
* @Date: 2020/4/15
*/
@RequestMapping(value ="center")
public String center(ModelMap map){
//此处调用类内部方法,获得数据,返回的是一个users的集合map,的视图
//在center2的ftl中呢,是使用了ftl的list标签,别名是user,然后获得user的每一个子项
map.put("users",parseUsers());
map.put("title","用户列表");
return "freemarker/center2";
}
//此处是一个类内部方法
private List<Map> parseUsers(){
List<Map> list= new ArrayList<>();
for(int i=0;i<100;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;
}
@Autowired
private Resource resource;//此处使用的是spring的注入方式
@RequestMapping("demo")//二级路径
public String hello(Model model){//返回视图层
//下面的三个选项是基于读取配置文件二得到的内容
model.addAttribute("name",resource.getName());
model.addAttribute("language",resource.getLanguage());
model.addAttribute("Website",resource.getWebsite());
return "freemarker/demo";
}
}
2、Freemarker工具类Resource
package com.uflinux.demo.util;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* @program: springbootdemo
* @description:
* @author: Feng_Lei
* @create: 2020-04-15 17:49
**/
//表示这个类是一个读取配置文件的类
@Data
@Configuration
//指定配置的一些属性,其中的prefix表示前缀
//大致代表读取文件中内容的key是什么,具体是什么是自定义的
@ConfigurationProperties(prefix = "com.uflinux")
/*
此处如果缺少【spring-boot-configuration-processor】的pom配置,则会出现【Spring引导配置注释处理器未配置】的错误
*/
//指定所读取的配置文件的路径
@PropertySource(value = "classpath:resource.properties")
public class Resource {
private String name ;
private String website;
private String language;
//...setter and getter
}
3、application.properties
spring:
mvc:
static-path-pattern:/static/**
view:
suffix:.html
prefix:/
freemarker:
cache: false #关闭模板缓存,方便测试
settings:
template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
#设定ftl文件路径
template-loader-path:classpath:/templates
#设定静态文件路径,js,css等
# freemarker静态资源配置
# 设定ftl文件路径
spring.freemarker.tempalte-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
4、resource.properties
实体类配置文件
com.uflinux.name=China
com.uflinux.website=www.baidu.top:18158/
com.uflinux.language=chinese
5、在templates/freemarker目录下,创建flt文件(PS:demo.ftl;center.ftl;center2.ftl)
center:
<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; }/*设置边框*/
</style>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Phone</th>
</tr>
<#-- <#list users as user>-->
<tr>
<td>${name}</td>
<td>${age}</td>
<td>${phone}</td>
</tr>
<#-- </#list>-->
</table>
</body>
</html>
center2 :
<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; }/*设置边框*/
</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>
demo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FreeMarker页面</title>
</head>
<body>
FreeMarker模板引擎
<h1>${name}</h1>
<h1>${language}</h1>
<h1>${Website}</h1>
</body>
</html>
分别得到的页面截图
1、index方法
2、center方法
3、demo方法