SpringBoot中有很多的starter:本质是多个JAR包集合

比如我们常用的:

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

其实它包含的内容有:

  <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.16.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.6.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

而每个依赖之下又包含有很多的JAR包,这里就不继续列举了

所以如果我们要用到的模板引擎不必去考虑需要什么JAR包

直接导入相对应的starter即可

模板引擎:

通常我们需要的是动态页面,动态页面就需要进行渲染

早期的项目使用JSP作为模板引擎,然后JSP是在后端进行,效率较低

JSP的优点:支持JSTL、El等方式,甚至可以直接写Java代码

JSP的缺点:效率问题,Undertow容器不支持,SpringBoot官网不推荐

Freemarker:严重依赖MVC模式,不依赖Servlet,不占用JVM

依赖:

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

配置:

#Freemarker配置
#本地测试不推荐使用缓存
spring.freemarker.cache=false
#编码
spring.freemarker.charset=UTF-8
#允许请求重写
spring.freemarker.allow-request-override=false
#进行路径检查
spring.freemarker.check-template-location=true
#格式为HTML
spring.freemarker.content-type=text/html
#暴漏request属性
spring.freemarker.expose-request-attributes=true
#暴漏session属性
spring.freemarker.expose-session-attributes=true
#文件后缀
spring.freemarker.suffix=.ftl
#路径
spring.freemarker.template-loader-path=classpath:/templates/

在templates下新建一个目录fm,新建一个ftl文件:index.ftl:

<!DOCTYPE html>
<html>
<head>
<title>Freemarker</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
Freemarker
</body>
</html>

注意:虽然页面在fm目录下,但配置的最后一项不可以写成classpath:/templates/fm

Controller:

package org.dreamtech.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
@RequestMapping("/hello")
private String index() {
return "fm/index";
}
}

访问localhost:8080/freemarker/hello即可看到index.ftl页面

如果需要动态渲染呢?

package org.dreamtech.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
@RequestMapping("/hello")
private String index(ModelMap modelMap) {
modelMap.addAttribute("user", new User("admin", "password"));
return "fm/index";
}
}
package org.dreamtech.springboot.controller;

public class User {
private String username;
private String password; public User(String username, String password) {
this.username = username;
this.password = password;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }
<!DOCTYPE html>
<html>
<head>
<title>Freemarker</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
Freemarker
Username:${user.username}
Password:${user.password}
</body>
</html>

Freemarker还有if else语法,循环语法等等,可以自行查找,这里就不介绍

Thymeleaf:SpringBoot官方推荐,适用于通常的项目,不适用于逻辑过于复杂的项目

直接以html作为文件结尾

依赖:

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

配置:

#Thymeleaf配置
#本地测试不推荐使用缓存
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
#前缀
spring.thymeleaf.prefix=classpath:/templates/
#后缀
spring.thymeleaf.suffix=.html
#编码
spring.thymeleaf.encoding=UTF-8

页面:

<!DOCTYPE html>
<html>
<head>
<title>Thymeleaf</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
Thymeleaf
<p>Username:</p>
<h3 th:text="${user.username}" />
<p>Password:</p>
<h3 th:text="${user.password}" />
</body>
</html>

Controller:

package org.dreamtech.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@RequestMapping("/index")
private String index(ModelMap modelMap) {
modelMap.addAttribute("user", new User("admin", "password"));
return "tl/index";
}
}

同样地,Thymeleaf还有很多其他语法,可以自行查找

实际开发中通常是前后端分离(Ajax

如果不是前后端分离,那么推荐使用:Thymeleaf

SpringBoot 2.x (8):模板引擎的更多相关文章

  1. SpringBoot第九集:整合JSP和模板引擎Freemarker/Thymeleaf(2020最新最易懂)

    SpringBoot第九集:整合JSP和模板引擎(2020最新最易懂) 当客户通过前端页面提交请求后,我们以前是怎么做的?后端接收请求数据,处理请求,把响应结果交给模板引擎JSP,最后将渲染后的JSP ...

  2. Spring Boot2(五):使用Spring Boot结合Thymeleaf模板引擎使用总结

    一.Thymeleaf概述 一般来说,常用的模板引擎有JSP.Velocity.Freemarker.Thymeleaf . SpringBoot推荐的 Thymeleaf – 语法更简单,功能更强大 ...

  3. Thymeleaf模板引擎的使用

    Thymeleaf模板引擎的使用 1.模板引擎 JSP.Velocity.Freemarker.Thymeleaf 2.springboot推荐使用Thymeleaf模板引擎 特点:语法更简单,功能更 ...

  4. JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎

    上篇博客我们聊了<JavaEE开发之SpringBoot工程的创建.运行与配置>,从上篇博客的内容我们不难看出SpringBoot的便捷.本篇博客我们继续在上篇博客的基础上来看一下Spri ...

  5. Springboot与Thymeleaf模板引擎整合基础教程(附源码)

    前言 由于在开发My Blog项目时使用了大量的技术整合,针对于部分框架的使用和整合的流程没有做详细的介绍和记录,导致有些朋友用起来有些吃力,因此打算在接下来的时间里做一些基础整合的介绍,当然,可能也 ...

  6. (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  7. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  8. SpringBoot系列: Pebble模板引擎

    ===============================Java 模板引擎选择===============================SpringBoot Starter项目向导中可选的J ...

  9. SpringBoot集成Thymeleaf模板引擎

    简单介绍 目前在JavaEE领域有几中比较常用的模板引擎,分别是Jsp.Velocity.Freemarker.Thymeleaf,对Freemark语法不是特别熟悉,不过对于前端页面渲染效率来说,j ...

  10. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

随机推荐

  1. 配置webpack中externals来减少打包后vendor.js的体积

    在日常的项目开发中,我们会用到各种第三方库来提高效率,但随之带来的问题就是打包后的vendor.js体积过大,导致加载时空白页时间过长,给用户的体验太差.为此我们需要减少vendor.js的体积,从本 ...

  2. codeforces 679e Bear and Bad Powers of 42

    传送门:http://codeforces.com/contest/679/problem/E 题目意思很清晰,给你一个序列,要求你完成以下三个操作: 1.输出A[i] 2.将[a,b]区间的所有数字 ...

  3. mmwave

    毫米波(mmWave) 致力于支持5G应用创新开发,集成在BEEcube BEE7基带平台上的赛灵思256QAM毫米波调制解调器IP为宽带回程原型设计提供完整的开箱即用型解决方案 赛灵思公司 (NAS ...

  4. INFO: Ignoring response <403 https://movie.douban.com/top250>: HTTP status code is not handled or not allowed

    爬取豆瓣电影top250,出现以下报错: 2018-08-11 22:02:16 [scrapy.core.engine] INFO: Spider opened 2018-08-11 22:02:1 ...

  5. hash与map的区别联系应用(转)

    一,hashtable原理: 哈希表又名散列表,其主要目的是用于解决数据的快速定位问题.考虑如下一个场景. 一列键值对数据,存储在一个table中,如何通过数据的关键字快速查找相应值呢?不要告诉我一个 ...

  6. SPOJ:One piece(不错的带权括号最大匹配问题)

    One of DB and TN common interests is traveling. One day, they went to Grand Line and found One Piece ...

  7. FTOUR2 - Free tour II

    传送门 题目翻译的很清楚……似乎点分治的题题目描述都非常简洁. 还是那个操作,一条路径要么全部在一棵子树中,要么经过当前的重心,所以考虑点分治. 首先dfs求出重心的每一棵子树中,有i个黑点的最长路径 ...

  8. 年少和 Smart の日常比赛 R3

    在洛谷上参加了个比赛....写写题解 rank3....共5人...(捂脸 没有注明是官方代码的均是我比赛时本人提交的代码 T1  洗牌 题目描述 小明把 n (n 为偶数)张牌按编号顺序 1, 2, ...

  9. ASP.NET Core:WebAppCoreReact

    ylbtech-ASP.NET Core:WebAppCoreReact 1.返回顶部 1. 2.   3.         4. 5. 6. 7. 2. wwwroot 返回顶部   3. Clie ...

  10. MySQL主从详细安装步骤

    网站: 程序在:web服务器192.168.1.100上面 数据库在:MySQL服务器192.168.1.123上面 实现目的:增加一台MySQL备份服务器(192.168.1.124),作为MySQ ...