自动配置和 thymeleaf模板引擎
目錄
自動配置
springboot自動配置原理:
在springboot,如何處理靜態資源
?thymeleaf模板引擎
特點
創建一個thymeleaf項目
在pom.xml中添加依賴
mybatis配置
熱部署代碼塊如下:
MySQL代碼塊如下:
這是所有的pom.xml文件內容(除了開頭)
自動配置
springboot自動配置原理:
1.springboot啟動會加載大量的自動配置類;
2.需要看一下我們需要的功能有沒有在springboot默認寫好的自動配置類中;
3.再看一下在自動配置類中到底配置了哪些組件(只要需要的組件存在其中,就不需要手動配置);、
4.給容器中自動配置類添加組件的時候,可以從properties類中獲取某些屬性。只需要在配置文件中添加指定屬性的值就可以了;
依舊接一篇文章:springboot的基本配置_程程呀是小白的博客-CSDN博客
在springboot,如何處理靜態資源
在springboot,我們可以使用兩種方式處理靜態資源:
1.webjars? ? ? ? localhost:8080/webjars/
2.public,static,resources,/**? ? ? ? ? ?localhost:8080/
優先級:
resources>static(默認)>public
?thymeleaf模板引擎
(首先了解一下模板引擎的作用(百度找的)
模板引擎的作用就是我們來寫一個頁面模板,比如有些值是動態的,我們寫一些表達式。而這些值從哪里來?我們來組裝一些數據,把這些數據找到,然后把這個模板和這個數據交給我們模板引擎,模板引擎按照我們這個數據幫你把這表達式解析、填充到我們指定的位置,然后把這個數據最終生成一個我們想要的內容給我們寫出去,這就是模板引擎,不管是jsp還是其他模板引擎,都是這個思想。只不過,就是說不同模板引擎之間,他們可能這個語法不一樣。SpringBoot給我們推薦的Thymeleaf是一個高級語言的模板引擎,它的語法簡單,功能強大。
怎么使用Thymeleaf
Thymeleaf模板引擎的官方地址、
1、Thymeleaf官網:Thymeleaf
2、Spring官方文檔:Thymeleaf
)
知道在springboot中,默認使用的是HTML頁面,jsp在springboot中默認已經取消掉了。雖然在經過配置還是可以使用,但是在springboot中推薦我們使用的是HTML頁面,但是HTML有一個弊端,就是數據信息沒有辦法在頁面獲取,所以thymeleaf幫助解決了這樣的問題
Thymeleaf是一個模板引擎,可以用來代替jsp頁面。之所以使用jsp頁面就是為了使用jstl或者ognl表達式獲取頁面數據
特點
1.使用方便,學習簡單,快速得實現表單得數據綁定。
2.Thymeleaf支持HTML原型,在服務不運行得情況下,可以直接運行,可以讓美工在瀏覽器上直接查看頁面的靜態效果,也可以支持開發人員在服務器運行時查詢動態頁面效果。
3.在html標簽中增加了額外得屬性來達到模版+數據得展示方式,在瀏覽器解析html頁面時,會自動忽略html標簽中未定義得屬性,達到可以顯示靜態頁面效果;當有數據返回時,thymeleaf標簽會動態得替換掉靜態內容,顯示動態頁面。
4.提供了標準和spring標準兩種語言,實現jstl,ognl表達式得效果。
創建一個thymeleaf項目
?
?
?
在pom.xml中添加依賴
加入熱部署,MySQL,web和thymeleaf模板
mybatis配置
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency>熱部署代碼塊如下:
<!--熱部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency>MySQL代碼塊如下:
<!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>?
這是所有的pom.xml文件內容(除了開頭)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ccy</groupId>
<artifactId>springboottext01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springboottext01</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
備注:本人自己學習的筆記,希望可以對其產生幫助
程程呀是小白的博客_CSDN博客-畢業季,vue,redis領域博主
總結
以上是生活随笔為你收集整理的自动配置和 thymeleaf模板引擎的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机定位那些事?
- 下一篇: Docker和Pycharm