首页
学习
活动
专区
圈层
工具
发布
25 篇文章
1
SpringBoot2.x系列教程(五十九)SpringBoot实现国际化i18n功能
2
SpringBoot2.x系列教程(五十五)Mybatis反向生成Java代码
3
SpringBoot2.x系列教程(四十四)WebSocket基础知识简介
4
SpringBoot2.x系列教程(四十三)SpringBoot整合Swagger2
5
SpringBoot2.x系列教程(五十)Spring Boot Idea中热部署(自动刷新)
6
SpringBoot2.x系列教程(四十二)SpringBoot中构建RESTful服务
7
SpringBoot2.x系列教程(三十八)SpringBoot配置Https访问
8
SpringBoot2.x系列教程(三十六)SpringBoot之Tomcat配置
9
SpringBoot2.x系列教程(三十四)Thymeleaf自动配置源码解析
10
SpringBoot2.x系列教程(三十三)Thymeleaf手动渲染实例讲解
11
SpringBoot2.x系列教程(三十二)Thymeleaf资源导入及公共布局
12
SpringBoot2.x系列教程(三十一)Thymeleaf的基本使用
13
SpringBoot2.x系列教程(三十)SpringBoot集成Thymeleaf
14
SpringBoot2.x系列教程(二十九)freemarker自动配置源码解析
15
SpringBoot2.x系列教程(二十八)freemarker基本语法使用
16
SpringBoot2.x系列教程(二十六)Springboot集成freemarker
17
SpringBoot2.x系列教程(二十五)Jsp中使用jstl和引入静态资源
18
SpringBoot2.x系列教程(二十三)SpringBoot集成Jsp
19
SpringBoot2.x系列教程(二十二)简单参数校验及统一异常处理
20
SpringBoot2.x系列教程(二十)自定义参数校验注解
21
SpringBoot2.x系列教程(十三)Jackson命名策略及自定义序列化
22
SpringBoot2.x系列教程(十)Json之基础使用详解
23
SpringBoot2.x系列教程(十九)Validation数据校验基础使用
24
SpringBoot2.x系列教程(九)基于Postman的RESTful接口调用
25
SpringBoot2.x系列教程(八)SpringBoot常用注解汇总

SpringBoot2.x系列教程(二十六)Springboot集成freemarker

FreeMarker简介

FreeMarker是一款模板引擎:即基于模板和数据源生成输出文本(html网页,配置文件,电子邮件,源代码)的通用工具。它是一个java类库。

FreeMarker最初被设计用来在MVC模式的Web开发框架中生成HTML页面,它没有被绑定到Servlet或HTML或任意Web相关的东西上。也可以用于非Web应用环境中。

模板编写使用FreeMarker Template Language(FTL)。使用方式类似JSP的EL表达式。模板中专注于如何展示数据,模板之外可以专注于要展示什么数据。

官网显示的使用图解:

当然,在非HTML场景下的模板使用也可以用如下图进行解释:

FreeMarker的特性

FreeMarker是基于Java的模板引擎,最初专注于使用MVC软件体系结构进行动态网页生成。使用Freemarker的主要优点是将表示层和业务层完全分开。

开发人员可以处理应用程序代码,而设计人员可以处理html页面设计。最终,使用freemarker可以将它们组合在一起以给出最终的输出页面。

FreeMarker的功能

  • 强大的模板语言:条件块,迭代,赋值,字符串和算术运算和格式,宏和函数(包括其他模板),默认转义(可选)等。
  • 多用途,轻量级:零依赖性,任何输出格式,可以从任何位置加载模板(可插拔),许多配置选项。
  • 国际化/本地化意识:区域设置敏感的数字和日期/时间格式,本地化的模板变体。
  • XML处理功能:将XML DOM放入数据模型并遍历它们,甚至进行声明式处理。
  • 通用的数据模型:Java对象通过可插拔适配器以变量树的形式暴露给模板,该适配器决定了模板如何使用。

总结一下优势就是:FreeMarker可将业务逻辑与表现层分离,有利于分工合作,提高开发效率。同时有利于提高访问速度,提升并发量,对SEO也更加友好。

SpringBoot集成

在pom文件中引入freemarker,当然在SpringBoot中是通过引入SpringBoot已经提供好的starter。完整的pom依赖引入如下:

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

其中spring-boot-starter-freemarker便是SpringBoot提供的starter,其间接引入了:

代码语言:javascript
复制
<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.28</version>
  <scope>compile</scope>
</dependency>

如果是通过Idea创建项目,可直接在创建时勾选。

这里大家需要注意的是此示例使用的SpringBoot版本不是2.2.2,而是2.1.5,至于为什么,后面章节会提到。这里我们先演示具体功能。

代码语言:javascript
复制
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starters</artifactId>
    <version>2.2.2.RELEASE</version>
</parent>

整个项目的目录结构如下:

代码语言:javascript
复制
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── secbro2
│   │   │           ├── SpringbootFreemarkerApplication.java
│   │   │           ├── controller
│   │   │           │   └── StudentController.java
│   │   │           └── entity
│   │   │               └── Student.java
│   │   └── resources
│   │       ├── application.properties
│   │       ├── static
│   │       └── templates
│   │           ├── biz
│   │           └── student.ftl

创建实体类:

代码语言:javascript
复制
@Data
public class Student {

	private String idNo;

	private String name;
}

创建Controller类并提供请求方法:

代码语言:javascript
复制
@Controller
public class StudentController {

	@GetMapping("/")
	public String getStudents(Model model) {

		List<Student> list = new ArrayList<>();

		Student s1 = new Student();
		s1.setIdNo("No1");
		s1.setName("Tom");
		list.add(s1);

		Student s2 = new Student();
		s2.setIdNo("No2");
		s2.setName("David");
		list.add(s2);

		model.addAttribute("students", list);
		return "student";
	}
}

这里返回了一个列表。页面相应的处理如下:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生列表</title>
</head>
<body>
<table border="1">
    <tr>
        <td>学生编号</td>
        <td>学生名称</td>
    </tr>
    <#list students as student>
        <tr>
            <td>${student.idNo}</td>
            <td>${student.name}</td>
        </tr>
    </#list>
</table>
</body>
</html>

访问对应的请求返回结果如下:

至此,关于Springboot集成freemarker的工作已经完成,所有配置均采用默认配置。

如果想进行定制化配置,还可以通过application.properties中进行配置。

代码语言:javascript
复制
# HttpServletRequest的属性是否可以覆盖controller中model的同名项
spring.freemarker.allow-request-override=false
# HttpSession的属性是否可以覆盖controller中model的同名项
spring.freemarker.allow-session-override=false
# 是否开启缓存
spring.freemarker.cache=false
# 模板文件编码
spring.freemarker.charset=UTF-8
# 是否检查模板位置
spring.freemarker.check-template-location=true
# Content-Type的值
spring.freemarker.content-type=text/html
# 是否将HttpServletRequest中的属性添加到Model中
spring.freemarker.expose-request-attributes=false
# 是否将HttpSession中的属性添加到Model中
spring.freemarker.expose-session-attributes=false
# 模板文件后缀
spring.freemarker.suffix=.ftl
# 模板文件位置
spring.freemarker.template-loader-path=classpath:/templates/
下一篇
举报
领券