SpringBoot筆記整理(三)

SpringBoot筆記整理(一)
SpringBoot筆記整理(二)
SpringBoot筆記整理(三)
SpringBoot筆記整理(四)

Web開發

1、使用SpringBoot:

1)創建SpringBoot應用,選中需要的模塊
2)SpringBoot已經默認將這些場景配置好了,只需要在配置文件中指定少量配置就可以運行起來
3)自己編寫業務代碼

自動配置原理

xxxAutoConfiguration:幫我們給容器中自動配置組件
xxxProperties:配置類來封裝配置文件的內容

2、SpringBoot對靜態資源的映射規則

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties {
    //可以設置和靜態資源有關的參數,緩存時間等
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
        CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
        if (!registry.hasMappingForPattern("/webjars/**")) {
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

        String staticPathPattern = this.mvcProperties.getStaticPathPattern();
        if (!registry.hasMappingForPattern(staticPathPattern)) {
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

    }
}

//配置歡迎頁映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
    WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
    welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
    return welcomePageHandlerMapping;
}

1)所有/webjars/**,都去classpath:/META-INF/resources//webjars/找資源
webjars:以jar包的方式引入靜態資源;
https://www.webjars.org/

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.4.1</version>
</dependency>

在這裏插入圖片描述

2)“/**”訪問當前項目的任何資源(靜態資源的文件夾)

"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":當前項目的根路徑

3)歡迎頁:靜態資源文件夾下的所有index.html頁面,被"/"映射
4)所有的
/favicon.ico 都是在靜態資源文件下找

3、模板引擎

SpringBoot推薦的Thymeleaf:語法更簡單,功能更強大
3.1 引入Thymeleaf

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3.2 Thymeleaf使用&語法

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    //只要把html頁面放在classpath:/templates/,thymeleaf就能自動渲染

使用:
3.2.1 導入thymeleaf的名稱空間

<html lang="en" xmlns:th="http://www/thymeleaf.org">

3.2.2 使用thymeleaf語法

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www/thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>success</h1>
    <!--th:text 將div裏面的文本內容設置爲-->
    <div th:text="${hello}">這裏顯示歡迎信息</div>
</body>
</html>

1)th:text:改變當前元素裏面的文本內容
th:任意html屬性,來替換原生屬性的值
在這裏插入圖片描述
2)表達式語法

Simple expressions:
    Variable Expressions: ${...}
    Selection Variable Expressions: *{...}
    Message Expressions: #{...}
    Link URL Expressions: @{...}
    Fragment Expressions: ~{...}
Literals
    Text literals: 'one text', 'Another one!',...
    Number literals: 0, 34, 3.0, 12.3,...
    Boolean literals: true, false
    Null literal: null
    Literal tokens: one, sometext, main,...
Text operations:
    String concatenation: +
    Literal substitutions: |The name is ${name}|
Arithmetic operations:
    Binary operators: +, -, *, /, %
    Minus sign (unary operator): -
Boolean operations:
    Binary operators: and, or
    Boolean negation (unary operator): !, not
Comparisons and equality:
    Comparators: >, <, >=, <= (gt, lt, ge, le)
    Equality operators: ==, != (eq, ne)
    Conditional operators:If-then: (if) ? (then)
    If-then-else: (if) ? (then) : (else)
    Default: (value) ?: (defaultvalue)
Special tokens:
    No-Operation: _
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章