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 ...
随机推荐
- ubuntu eclipse 无法打开
报错: The catalog could not be loaded... 这个问题,我上网查了很久.. 基本上网上可用的办法我都试过了.全是失败的 firefox+autoproxy,shadow ...
- Cuba项目配置IDEA,如何dubug
1.在cuba中编辑端口,如下图: 2.在IDEA中添加一个远程链接,添加为对应的端口号,然后运行debug即可 如果两个端口号不对应,就会报出以下的错误:unable to open debugge ...
- 自学Zabbix3.11-宏Macros
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix3.11-宏Macros zabbix宏变量让zabbix变得更灵活,它根据一系列 ...
- 【UOJ#275】组合数问题(卢卡斯定理,动态规划)
[UOJ#275]组合数问题(卢卡斯定理,动态规划) 题面 UOJ 题解 数据范围很大,并且涉及的是求值,没法用矩阵乘法考虑. 发现\(k\)的限制是,\(k\)是一个质数,那么在大组合数模小质数的情 ...
- 【BZOJ2159】Crash的文明世界(第二类斯特林数,动态规划)
[BZOJ2159]Crash的文明世界(第二类斯特林数,动态规划) 题面 BZOJ 洛谷 题解 看到\(k\)次方的式子就可以往二项式的展开上面考,但是显然这样子的复杂度会有一个\(O(k^2)\) ...
- rt-thread learning notes
rt-thread learning notes 2018-01-15 > 001 具有相同优先级的线程,每个线程的时间片大小都可以在初始化或创建该线程时指定 rt_thread_t rt_th ...
- luogu1169 棋盘制作 (单调栈)
先预处理出来从每个位置 以0开始 往右交替最多能放多少格 然后就相当于对每一列做HISTOGRA #include<bits/stdc++.h> #define pa pair<in ...
- 逆向---01.Nop、中文字符串搜索、保存修改后程序
基础知识:(Nop:删除跳转) gcc编译链接命令: gcc -o 生成文件名 源文件名 gcc编译成汇编代码:gcc -o 生成文件名 -S 源文件名 VS查看汇编代码:(调试模式下,Ctrl+F ...
- SpringCloud学习(二)---Eureka
Eureka 重点在使用,概念和源码基本不涉及 Eureka是一个基于REST(REST是HTTP协议的)的服务,主要在亚马逊网络服务(AWS)云中使用,定位服务来进行中间层服务器的均衡负载和故障转移 ...
- [POI2012]BON-Vouchers----你敢模拟吗?
链接:https://www.luogu.org/problemnew/show/P3536 题意: 定义n个数为幸运数字,一共有n批人,设第i批人有x个,则它们会依次取走余下的x的倍数中最小的x个, ...