SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf 4节课
1、SpringBoot Starter讲解
简介:介绍什么是SpringBoot Starter和主要作用
1、官网地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-starter
2、starter主要简化依赖用的
spring-boot-starter-web->里面包含多种依赖
3、几个常用的starter
spring-boot-starter-activemq
spring-boot-starter-aop
spring-boot-starter-data-redis
spring-boot-starter-freemarker
spring-boot-starter-thymeleaf
spring-boot-starter-webflux
2、SpringBoot2.x常见模板引擎讲解和官方推荐使用
简介:介绍常用的SpringBoot2.x模板引擎和官方推荐案例
1、JSP(后端渲染,消耗性能)
Java Server Pages 动态网页技术,由应用服务器中的JSP引擎来编译和执行,再将生成的整个页面返回给客户端
可以写java代码
持表达式语言(el、jstl)
内建函数
JSP->Servlet(占用JVM内存)permSize
javaweb官方推荐
springboot不推荐 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations
2、Freemarker
FreeMarker Template Language(FTL) 文件一般保存为 xxx.ftl
严格依赖MVC模式,不依赖Servlet容器(不占用JVM内存)
内建函数
3、Thymeleaf (主推)
轻量级的模板引擎(处理复杂逻辑业务的不推荐,解析DOM或者XML会占用多的内存)
可以直接在浏览器中打开且正确显示模板页面
直接是html结尾,直接编辑
xdlcass.net/user/userinfo.html
社会工程学
伪装
3、SpringBoot2.x整合模板引擎freemarker实战
简介:SpringBoot2.x整合模板引擎freemarker实战
1、Freemarker相关maven依赖
<!-- 引入freemarker模板引擎的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2、Freemarker基础配置
# 是否开启thymeleaf缓存,本地为false,生产建议为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
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.suffix=.ftl
#路径
spring.freemarker.template-loader-path=classpath:/templates/
3、建立文件夹(通常在src/main/resources/templates/下)
1)src/main/resources/templates/fm/user/
2)建立一个index.ftl
3)user文件夹下面建立一个user.html
4、简单测试代码编写和访问
代码示例:
application.properties:
web.images-path=L:/images
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path}
#指定某些文件不进行监听,即不会进行热加载devtool(重启后不会监听下面这个文件)
#spring.devtools.restart.exclude=application.properties #通过触发器,去控制什么时候进行热加载部署新的文件
spring.devtools.restart.trigger-file=trigger.txt server.port=8083 #文件上传路径配置
web.file.path=L:/images #测试配置文件注入
test.name=www.xdclass.net
test.domain=springboot #自定义启动banner的路径
spring.banner.location=banner.txt # 是否开启freemarker缓存,本地为false,生产建议为true
spring.freemarker.cache=false spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
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.suffix=.ftl
#路径
spring.freemarker.template-loader-path=classpath:/templates/
FreemakerController.java:
package net.xdclass.demo.controller; import net.xdclass.demo.domain.ServerSettings; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/freemaker")
public class FreemakerController { @Autowired
private ServerSettings setting; @GetMapping("hello")
public String index(ModelMap modelMap){ modelMap.addAttribute("setting", setting); return "fm/index"; //不用加后缀,在配置文件里面已经指定了后缀
} }
src/main/resources/templates/fm/index.ftl:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
freemaker整合,index.html页面
<h1>xdclass.net</h1> <h1> ${setting.name} </h1>
<h1> ${setting.domain} </h1> </body>
</html>
浏览器访问:http://localhost:8083/freemaker/hello
结果:
freemaker整合,index.html页面
xdclass.net
www.xdclass.net
springboot
4、SpringBoot2.x整合模板引擎thymeleaf实战
讲解:SpringBoot2.x整合模板引擎thymeleaf实战
官网地址:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html
1、thymeleaf相关maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、thymeleaf基础配置
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
#前缀
spring.thymeleaf.prefix=classpath:/templates/
#编码
spring.thymeleaf.encoding=UTF-8
#类型
spring.thymeleaf.content-type=text/html
#名称的后缀
spring.thymeleaf.suffix=.html
3、建立文件夹
1)src/main/resources/templates/tl/
2)建立一个index.html
4、简单测试代码编写和访问
注意:$表达式只能写在th标签内部
快速入门:https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html
代码示例:
application.properties:
web.images-path=L:/images
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path},classpath:/templates/
#指定某些文件不进行监听,即不会进行热加载devtool(重启后不会监听下面这个文件)
#spring.devtools.restart.exclude=application.properties #通过触发器,去控制什么时候进行热加载部署新的文件
spring.devtools.restart.trigger-file=trigger.txt server.port=8083 #文件上传路径配置
web.file.path=L:/images #测试配置文件注入
test.name=www.xdclass.net
test.domain=springboot #自定义启动banner的路径
spring.banner.location=banner.txt # 是否开启freemarker缓存,本地为false,生产建议为true
#spring.freemarker.cache=false
#
#spring.freemarker.charset=UTF-8
#spring.freemarker.allow-request-override=false
#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.suffix=.ftl
##路径
#spring.freemarker.template-loader-path=classpath:/templates/ #开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
#前缀
spring.thymeleaf.prefix=classpath:/templates/
#编码
spring.thymeleaf.encoding=UTF-8
#类型
spring.thymeleaf.servlet.content-type=text/html
#spring.thymeleaf.content-type=text/html
#名称的后缀
spring.thymeleaf.suffix=.html
ThymeleafController.java:
package net.xdclass.demo.controller; import net.xdclass.demo.domain.ServerSettings; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/tyhmeleaf")
public class ThymeleafController { @Autowired
private ServerSettings setting; @GetMapping("hello")
public String index(){ return "index"; //不用加后缀,在配置文件里面已经指定了后缀
} @GetMapping("info")
public String admin(ModelMap modelMap){ modelMap.addAttribute("setting", setting); return "tl/admin/info"; //不用加后缀,在配置文件里面已经指定了后缀
}
}
src/main/resources/templates/tl/admin/info.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
模板引擎整合thymeleaf admin/info.html <h1 >测试内容,未加th表达式</h1>
<h1 th:text="${setting.name}">测试内容</h1>
<h1>xdclass.net</h1>
</body>
</html>
测试结果对比:
浏览器访问:http://localhost:8083/tyhmeleaf/info
结果:
测试内容,未加th表达式
www.xdclass.net
xdclass.net
浏览器访问:http://localhost:8083/tl/admin/info.html
结果:
模板引擎整合thymeleaf admin/info.html
测试内容,未加th表达式
测试内容
xdclass.net
========7、SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf 4节课=========================
加入小D课堂技术交流答疑群:Q群:699347262
1、SpringBoot Starter讲解简介:介绍什么是SpringBoot Starter和主要作用
1、官网地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-starter2、starter主要简化依赖用的spring-boot-starter-web->里面包含多种依赖
3、几个常用的starterspring-boot-starter-activemqspring-boot-starter-aopspring-boot-starter-data-redisspring-boot-starter-freemarkerspring-boot-starter-thymeleafspring-boot-starter-webflux
2、SpringBoot2.x常见模板引擎讲解和官方推荐使用简介:介绍常用的SpringBoot2.x模板引擎和官方推荐案例
1、JSP(后端渲染,消耗性能)Java Server Pages 动态网页技术,由应用服务器中的JSP引擎来编译和执行,再将生成的整个页面返回给客户端可以写java代码持表达式语言(el、jstl)内建函数JSP->Servlet(占用JVM内存)permSizejavaweb官方推荐springboot不推荐 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations
2、Freemarker FreeMarker Template Language(FTL) 文件一般保存为 xxx.ftl严格依赖MVC模式,不依赖Servlet容器(不占用JVM内存)内建函数
3、Thymeleaf (主推)轻量级的模板引擎(负责逻辑业务的不推荐,解析DOM或者XML会占用多的内存)可以直接在浏览器中打开且正确显示模板页面
直接是html结尾,直接编辑xdlcass.net/user/userinfo.html社会工程学伪装
3、SpringBoot2.x整合模板引擎freemarker实战简介:SpringBoot2.x整合模板引擎freemarker实战
1、Freemarker相关maven依赖<!-- 引入freemarker模板引擎的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2、Freemarker基础配置# 是否开启thymeleaf缓存,本地为false,生产建议为truespring.freemarker.cache=false
spring.freemarker.charset=UTF-8spring.freemarker.allow-request-override=falsespring.freemarker.check-template-location=true#类型spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=truespring.freemarker.expose-session-attributes=true#文件后缀spring.freemarker.suffix=.ftl#路径spring.freemarker.template-loader-path=classpath:/templates/
3、建立文件夹1)src/main/resources/templates/fm/user/2)建立一个index.ftl3)user文件夹下面建立一个user.html
4、简单测试代码编写和访问
4、SpringBoot2.x整合模板引擎thymeleaf实战讲解:SpringBoot2.x整合模板引擎thymeleaf实战
官网地址:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html1、thymeleaf相关maven依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
2、thymeleaf基础配置
#开发时关闭缓存,不然没法看到实时页面spring.thymeleaf.cache=falsespring.thymeleaf.mode=HTML5#前缀spring.thymeleaf.prefix=classpath:/templates/#编码spring.thymeleaf.encoding=UTF-8#类型spring.thymeleaf.content-type=text/html#名称的后缀spring.thymeleaf.suffix=.html
3、建立文件夹1)src/main/resources/templates/tl/2)建立一个index.html
4、简单测试代码编写和访问注意:$表达式只能写在th标签内部快速入门:https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html
SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf 4节课的更多相关文章
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第7节 SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf_28..SpringBoot Starter讲解
笔记 1.SpringBoot Starter讲解 简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-b ...
- 【SpringBoot】常用Starter介绍和整合模板引擎Freemaker、thymeleaf
========7.SpringBoot常用Starter介绍和整合模板引擎Freemaker.thymeleaf ========================= 1.SpringBoot Sta ...
- springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息
1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...
- SpringBoot入门系列(四)整合模板引擎Thymeleaf
前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...
- SpringBoot第九集:整合JSP和模板引擎Freemarker/Thymeleaf(2020最新最易懂)
SpringBoot第九集:整合JSP和模板引擎(2020最新最易懂) 当客户通过前端页面提交请求后,我们以前是怎么做的?后端接收请求数据,处理请求,把响应结果交给模板引擎JSP,最后将渲染后的JSP ...
- Spring Boot整合模板引擎jsp
jsp也算是一种模板引擎吧.整合jsp前,先说一下运行SpringBoot项目的几种方式 1. 运行SpringBoot项目的几种方式 1.1 使用内嵌Tomcat运行项目 在IDE中右键运行启动类, ...
- SpringBoot系列:Spring Boot使用模板引擎FreeMarker
一.Java模板引擎 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 在jav ...
- SpringBoot系列:Spring Boot使用模板引擎Thymeleaf
一.Java模板引擎 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 在jav ...
- SpringBoot系列:Spring Boot使用模板引擎JSP
一.Java模板引擎 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 在jav ...
随机推荐
- FieldGroup绑定ItemDataSource
FieldGroup可以直接绑定一个数据源DataSource.但如果想绑定某个值,并没有直接作为数据库中的一个字段存在.而是最后转为json串保存在数据库中.这样的话相当于key-value模式的D ...
- Centos7安装Zabbix4.0步骤
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 Centos7安装Zabbix4.0步骤 官方搭建zabbix4.0的环境要求: 1. 环境搭建L ...
- Android 安装 卸载 更新 程序
安装程序的方法: .通过Intent机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据. 1. String fileName =Environment.getExternalStorage ...
- [luogu3834]静态区间第k小【主席树】
传送门:https://www.luogu.org/problemnew/show/P3834 题目描述 如题,给定N个整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 分析 很多人都说是用 ...
- asp <----> vb(com,dll) <---> c 来回的调用,生命在于折腾
最近想改进一个两年前写的小程序,原因是最近将运行在托管服务器上的asp程序迁移到阿里云主机上运行. 初次使用阿里云主机,买了一个配置较低的主机(1核,1G内存,年付:1500左右吧) 原来使用的托管的 ...
- malloc创建三维数组
#include <stdio.h> #include <stdlib.h> #include <malloc.h> int main() { //f[0],f[] ...
- C#面向对象设计的七大原则
本文我们要谈的七大原则,即:单一职责,里氏替换,迪米特法则,依赖倒转,接口隔离,合成/聚合原则,开放-封闭 . 1. 开闭原则(Open-Closed Principle, OCP) 定义:软件实 ...
- pyqt5 设置窗口按钮等可用与不可用
setEnabled(True) 设置窗口或者按钮可用,Flase不可用
- 学习windows编程 day4 之 多边矩形填充
#include <windows.h> #include <math.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, ...
- redis的入门篇----启动和关闭
废话不多说 本博主要的内容是redis的常见命令 redis分为服务端和客户端 服务端的启动方式: redis-server redis-server ${redis.conf} //后面是一 ...