1. 程式人生 > 實用技巧 >3.Springboot(三)

3.Springboot(三)

靜態資源

搜尋webMvcConfiguration看原始碼,找到addResourceHandlers方法,新增靜態資源

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).setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));
                }

                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).setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));
                }

            }
        }

由以上程式碼可以看出新增靜態資源由三種方式:

  • 方式一:
    • 在application.properties中自定義靜態資原始檔路徑,當自定義靜態資源的路徑之後,系統自帶的配置檔案的路徑就失效了
spring.mvc.static-path-pattern=/cdl/...
  • 方式二

    • 使用 webjars,然後去webjars官網查詢相關的靜態資源的maven配置,然後通過路徑/webjars/**訪問,
  • 方式三
    可以看出只要是在resources包下面的都可以直接訪問到

首頁定製和圖示定製

在resource資料夾下面,任意路徑定義一個index.html就可以通過local+埠號訪問到改也買你,不用加路徑

在resources檔案加下定義favicon.ico檔案,然後在配置檔案關閉預設圖示即可,

spring.mvc.favicon.enabled = false 

thymeleaf 模板引擎 freemark

1.匯入thymeleaf的spingboot啟動器

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

2.在templates資料夾下新建html檔案,並匯入thymeleaf依賴

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

3.編寫測試類

@Controller
public class TestController {

    @RequestMapping("/t1")
    public String test1(Model model){
        model.addAttribute("msg","Hello,Thymeleaf");
        return "test";
    }
}
<!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中的內容設定為它指定的值,和之前學習的Vue一樣-->
<div th:text="${msg}"></div>
</body>
</html>

thymeleaf模板語法

MVC配置原理

使用一個類全面擴充套件springMVC
1.在包下新建一個資料夾,config
2.在資料夾下新建一個類,例如:myMvcConfig
3.添加註解@configuration,然後實現webMvcConfigurer介面(可以發現下面有很多可以重寫的方法)


如果想要定製一些功能,只要寫一個元件,然後交給springboot,springboot就會幫我們自動裝配

例如:定製一個檢視解析器

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    //實現類檢視直譯器介面,可以看做就是一個檢視解析器
    @Bean
    public ViewResolver myViewResolver(){
        return new MyViewResolver();
    }

    //自定義一個檢視解析器
    public static class MyViewResolver implements ViewResolver {
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }
}

擴充套件SpingMVC

接管mvc的功能
例如:請求直接跳轉頁面

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("test");
    }
}