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 ...
随机推荐
- HDU5387-模拟水题
模拟钟表的时分秒针的走动,给出时间求出夹角.注意每组输出要有一个空格 以后要想好再写代码,这样一个水题做了50分钟,太弱了... #include<cstdio> #include< ...
- 【总结】 Lucas定理
\(Lucas\)定理: \(C^x_y≡C^{x/p}_{y/p}*C^{x\%p}_{y\%p} ~~(mod~p)\) 证明不会2333 void pre(){ A[0]=A[1]=B[0]=B ...
- hihoCoder#1743:K-偏差排列(矩阵快速幂+状压dp)
题意 如果一个 \(1\to N\) 的排列 \(P=[P_1, P_2, ... P_N]\) 中的任意元素 \(P_i\) 都满足 \(|P_i-i| ≤ K\) ,我们就称 \(P\) 是 \( ...
- 7种JVM垃圾收集器特点,优劣势、及使用场景
今天继续JVM的垃圾回收器详解,如果说垃圾收集算法是JVM内存回收的方法论,那么垃圾收集器就是内存回收的具体实现. 一.常见的垃圾收集器有3类 1.新生代的收集器包括 Serial PraNew Pa ...
- ANDROID OptionMenu 菜单列表
package com.app.menu; import android.os.Bundle; import android.app.Activity; import android.content. ...
- 【转】安全加密(二):BLE安全攻击反制措施
本文导读 近年来出现了越来越多的低功耗蓝牙应用,即BLE(Bluetooth Low Energy),比如说智能手环.防丢器等,对蓝牙的安全要求也越来越高.这篇文章将深入浅出说明BLE4.0~4.2中 ...
- OneProxy构建
OneProxy的主要功能有:1. 垂直分库 2. 水平分表 3. Proxy集群 4. 读高可用 5. 读写分离(master不参与读) 6. 读写分离(master参与读) 7. 写高可用 8. ...
- Java核心技术-映射
集是一个集合,它可以快速地查找现有的元素.但是,要查看一个元素,需要有要查找元素的精确副本.这不是一种非常通用的查找方式.通常,我们知道某些键的信息,并想要查找与之对应的元素.映射(map)数据结构就 ...
- java:取屏幕大小(去掉任务栏的高度部分)
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); Insets screenInsets = Toolkit.getD ...
- GBDT用于分类问题
一.简介 GBDT在传统机器学习算法里面是对真实分布拟合的最好的几种算法之一,在前几年深度学习还没有大行其道之前,gbdt在各种竞赛是大放异彩.原因大概有几个 一:效果确实挺不错. 二:既可以用于分类 ...