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
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
社會工程學
僞裝
第三集 SpringBoot整合模板引擎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、建立文件夾
1)src/main/resources/templates/fm/user/
2)建立一個index.ftl
3)user文件夾下面建立一個user.html
4、簡單測試代碼編寫和訪問
SpringBoot2整合模板引擎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
公衆號: 自學it的攻城獅(id:study458)