SpringBoot(三):Web開發
引入依賴
在使用SpringBoot開發web時,我們先得引入web模組的啟動器的依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
一、Spring Boot Web開發的預設配置
1.靜態資源的對映規則:
靜態資源又分為webjars和普通的靜態資源。
WebJars是將Web前端Javascript和CSS等資源打包成Java的Jar包,這樣在Java Web開發中我們可以藉助Maven這些依賴庫的管理,保證這些Web資源版本唯一性。
我們可以在WebMVCAutoConfiguration類中的靜態類的WebMvcConfigurerAdpter類中的addResourceHandlers方法。以下是原始碼:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"})
.addResourceLocations (new String[]{"classpath:/META-INF/resources/webjars/"})
.setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern})
.addResourceLocations(this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));
}
}
}
我們可以看到SpringBoot預設掃描的的webjars資料夾為classpath:/META-INF/resources/webjars資料夾下(所有的jar下的/META-INF/resources/webjars的資料夾)。
所有“/webjars/*” 請求 ,都去 classpath:/META-INF/resources/webjars/ 找資源
而預設的靜態檔案的資料夾則需要在ResourceProperties類的StaticLocations屬性中進一步查詢。
靜態程式碼塊
建構函式
屬性定義
private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[]{"/"};
private static final String[] CLASSPATH_RESOURCE_LOCATIONS =
new String[]{"classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private static final String[] RESOURCE_LOCATIONS;
private String[] staticLocations;
可知訪問當前專案的任何資源("/*" ),都去以下資料夾找對映
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
- “/”:當前專案的根路徑
2.歡迎頁的對映規則
"/"請求會查詢所有靜態資原始檔夾下面的所有index.html檔案。
3.favicon的對映規則
所有的"**/favicon.ico "都是在靜態資原始檔夾下查詢。
4.SpringMVC的自動配置
(1)SpringMVC的自動配置
SpringBoot自動配置好了SpringMVC。以下是SpringBoot對SpringMVC的預設配置(WebMVCAutoConfiguration類中):
- Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.(包含了ContentNegotiatingViewResolverd兩個檢視解析器)
- Support for serving static resources, including support for WebJars .(支援靜態資源和WebJars,就是前三個介紹的)
- Automatic registration of Converter, GenericConverter, Formatter beans.(自動註冊了轉換器,類轉換器,格式化器)
- Automatic use of a ConfigurableWebBindingInitializer bean(自動使用ConfigurableWebBindingInitializer)
(2)擴充套件SpringMVC
SpringBoot的自動配置是符合我們大多數需求的,在你既需要保留SpringBoot提供的便利又需要增加自己的額外的配置的時候,可以定義一個配置類並繼承WebMvcConfigurerAdapter。
(3)接管SpringBoot的Web配置
如果SpringBoot提供的SpringMVC預設配置不符合你的需求,則可以通過一個配置類(註解有@Configuration的類)加上@EnableWebMvc註解來實現自己控制MVC的配置。
二、模板引擎
JSP、Velocity、Freemarker、Thymeleaf都是模板引擎。可以用一下的圖來解釋什麼是模板引擎。
模板引擎的功能就是將資料填充到頁面中。
SpringBoot推薦使用Thymeleaf。Thymleaf語法更簡單,功能更加強大。
1、在SpringBoot中使用Thymeleaf首先得引入Thymeleaf。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<!--預設版本是2.1.6-->
</dependency>
<!--以下設定可以切換thymeleaf版本-->
<properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 佈局功能的支援程式 thymeleaf3主程式 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
2、Thymeleaf使用
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
我們查看了Thymeleaf的自動配置中properties類可知:
只要我們把HTML頁面放在classpath:/templates,Thymeleaf會預設掃描這個路徑進行渲染。
我們在html檔案中需要引入Thymeleaf的名稱空間。
<html lang="en" xmlns:th="http://www.thymeleaf.org">
編寫html檔案
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
<!--th:text 將div裡面的文字內容設定為 -->
<div th:text="${hello}">這是顯示歡迎資訊</div>
</body>
</html>
3.語法規則
一些常用的標籤