一、Thymeleaf和FreeMarker优缺点
1.1Thymeleaf
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码,开发者只需将标签属性添加到模板中即可。接下来,这些标签属性就会在DOM(文档对象模型)上执行预先制定好的逻辑。
优点
1、静态html嵌入标签属性,浏览器可以直接打开模板文件,便于前后端联调。
2、springboot官方推荐方案。
缺点
1、模板必须符合xml规范
2、html必须添加<html xmlns:th="http://www.thymeleaf.org">
1.2FreeMarker
FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。目前企业中:主要用Freemarker做静态页面或是页面展示
优点
1、不能编写java代码,可以实现严格的mvc分离
2、性能非常不错
3、对jsp标签支持良好
4、内置大量常用功能,使用非常方便
5、宏定义(类似jsp标签)非常方便
6、使用表达式语言
缺点
1、不是官方标准
2、用户群体和第三方标签库没有jsp多</pre>
二、thymeleafDemo应用
2.1Springboot的pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf模版引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
2.2默认配置文件(支持yml格式)
spring:
thymeleaf:
cache: false
# 以下是默认配置
# prefix: classpath:/templates/
# suffix: .html
# mode: HTML5
# encoding: UTF-8
# content-type: text/html
2.3创建启动类
@Controller
@SpringBootApplication
public class SpringbootThymeleafApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootThymeleafApplication.class,args);
}
@RequestMapping("/")
public String hello(Map<String,Object> map){
map.put("name","xiaoqiaobian");
return "hello";
}
}
2.4创建XML/XHTML/HTML5模版
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello World!</title>
</head>
<body>
Hello World <p th:text="${name}"></p>
</body>
</html>
2.5运行启动类

访问http://localhost:8080/
demo测试成功

三、FreeMarker应用
3.1Springboot的pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--freemarker模版引擎依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
2.2创建application.properties手动添加配置文件
## Freemarker 配置
# 设置缓存开启状态
spring.freemarker.cache= false
# 模版的路径
spring.freemarker.template-loader-path= classpath:/templates/ftl/
# 设置页面编码格式
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
2.3创建启动类
@SpringBootApplication
public class SpringbootFreemarkerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFreemarkerApplication.class,args);
}
}
2.4创建AppContoller
@Controller
public class AppController {
@RequestMapping("/")
public String hello(Map<String,Object> map){
map.put("name","xiaoqiaobian");
return "lucky";
}
}
2.5创建JSP/XHTML/HTML5模版
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是一个jsp</title>
</head>
<body>
Lucky ${name}!!!!
</body>
</html>
2.5运行启动类

访问http://localhost:8080/
demo测试成功

网友评论