1. 程式人生 > >SpringBoot的Web開發

SpringBoot的Web開發

頁面 src 1.2 tom spring servlet web style per

Web開發是開發中至關重要的一部分,web開發的核心內容主要包括servelet容器和SpringMVC。

1.SpringBoot的Web開發支持。

SpringBoot提供了spring-boot-starter-web為web開發予以支持,spring-boot-starter-web提供了內嵌的Tomcat以及SpringMVC的依賴

而web相關的自動配置存儲在spring-boot-autoconfigure.jar的org.srpingframework.boot.autoconfigure.web

技術分享圖片

技術分享圖片

2.Thymeleaf模板引擎

技術分享圖片

1.引入Thymeleaf

下面的代碼是一個基本的Thymeleaf模板頁面,在這裏我們引入了Bootstrap(作為樣式控制)和jQuery(DOM操作),當然他們不是必須的。

技術分享圖片

技術分享圖片

2.訪問model中的數據

通過"${}"訪問model中的屬性,這個JSP極為相似。

技術分享圖片

model中的數據叠代

技術分享圖片

技術分享圖片

4.數據判斷

技術分享圖片

5.JavaScript中訪問model

技術分享圖片

1.2與SpringMVC集成

在springMVC中,若我們需要集成一個模板引擎的話,需要定義ViewResolver,而ViewResolver需要定義一個view。

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

1.3springboot對Thymeleaf支持

理解SpringMVC和Thymeleaf集成的原理。但在springboot中這一切都是不需要的,SpringBoot通過autoconfigur.thymeleaf包對Thymeleaf進行自動配置。

技術分享圖片

3.Web相關的配置

1.SpringBoot提供的自動配置

通過查看WebMvcAutoConfiguration及WebMvcProperties的源碼,可以發現SpringBoot為我們提供了如下自動配置。

1.自動配置的ViewResolver

技術分享圖片

技術分享圖片

2.接管SpringBoot的Web配置

如果springBoot提供的SpringMVC不符合要求,則可以通過一個配置類,(註解有@Configuration的類)加上@EnableWebMVC註解來實現 完全自己控制的MVC配置。

技術分享圖片

技術分享圖片

技術分享圖片

3.註冊Servlet,Filter,listener

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

SpringBoot的Web開發