欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot+Thymeleaf靜態(tài)資源的映射規(guī)則說明

 更新時間:2021年11月11日 11:21:44   作者:qq_39602928  
這篇文章主要介紹了SpringBoot+Thymeleaf靜態(tài)資源的映射規(guī)則說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Spring Boot中靜態(tài)資源的映射規(guī)則

Spring Boot中靜態(tài)資源主要包括兩部分:1、webjars資源,2、自定義的其他html、css、js資源,下面分別介紹兩種資源的映射規(guī)則。

1)、webjars資源

WebJars是將web前端資源(js,css等)打成jar包文件,然后借助Maven工具,以jar包形式對web前端資源進(jìn)行統(tǒng)一依賴管理,保證這些Web資源版本唯一性。webjars導(dǎo)入對應(yīng)的xml代碼可以在webjars的官網(wǎng)進(jìn)行復(fù)制。

https://www.webjars.org/

在這里插入圖片描述

SpringBoot對webjars資源的映射規(guī)則在WebMvcAutoConfiguration.java包里面,代碼部分截圖如下所示:

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));
        }
    }
}

由上述代碼可以看出,所有訪問項(xiàng)目根目錄下的webjars下的所有資源,都會被映射到classpath:/META-INF/resources/webjars/文件夾下,其中classpath是resources資源文件夾或類的根目錄。

而使用maven方式導(dǎo)入的webjars包的靜態(tài)資源(js、css)會自動放到classpath:/META-INF/resources/webjars/文件夾下,如下所示:

在這里插入圖片描述

由圖可以看出dist中的靜態(tài)資源文件上層目錄為resources/webjars/**

因此在前端引用(此處用的是thymeleaf模板引擎)時可以用如下方式引用:直接從webjars目錄開始寫即可,上述靜態(tài)資源映射配置的實(shí)際效果即在自己寫的資源路徑前面加上classpath:/META-INF/resources前綴

<link  th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="external nofollow"  rel="stylesheet">

2)、自己添加的靜態(tài)資源文件

Spring Boot對自己添加的靜態(tài)資源文件的映射規(guī)則仍然在WebMvcAutoConfiguration.java文件中,實(shí)際配置是在ResourcesProperties.java資源文件中CLASSPATH_RESOURCE_LOCATIONS變量。

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

上述配置效果即所有的靜態(tài)資源的訪問除直接訪問/webjars/**路徑時,會在訪問路徑前加上上述配置中的任意一個字符串進(jìn)行查找,直到在相應(yīng)的文件下找到對應(yīng)的靜態(tài)資源。因此在編寫SpringBoot應(yīng)用時一般可以將靜態(tài)資源放置在resources資源目錄下的static或者public文件夾下,如下圖所示:

在這里插入圖片描述

如上圖若想訪問layui.js可以如下引用靜態(tài)資源:

<script  th:src="@{/layui/layui.js}"></script>

上述引用之后,SpringBoot會去CLASSPATH_RESOURCE_LOCATIONS變量指定的路徑下找layui/layui.js直到找到該文件位置。

CLASSPATH_RESOURCE_LOCATIONS的值可以直接在SpringBoot的配置文件application.properties或yml文件中進(jìn)行修改。

Thymeleaf模板引擎的映射規(guī)則

Thymeleaf模板引擎的映射規(guī)則如下所示

@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";

上述代碼的實(shí)際效果是在使用模板引擎是會將請求路徑字符串的前面加上classpath:/templates/,后面加上html進(jìn)行資源請求。因此一般將需要模板引擎進(jìn)行渲染的html界面放在resources路徑下的templates文件夾下,并且在請求的路徑字符串中不需要加html后綴。

一個簡單的例子如下所示:

    @RequestMapping("/success")
    public String success(Map<String,Object> map){
//        map=new HashMap<>();  這個地方不能再new了
        map.put("hello","hello");
        return "success";
    }

上述代碼返回所渲染的html界面即位于resources/templates文件夾下的success.html界面。該默認(rèn)設(shè)置頁可以直接在配置文件中通過spring.thymeleaf選項(xiàng)進(jìn)行修改。

SpringBoot對靜態(tài)資源的映射規(guī)則源碼學(xué)習(xí)筆記

在這里插入圖片描述

WebMvcAuotConfiguration:

		@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
			if (!this.resourceProperties.isAddMappings()) {
				logger.debug("Default resource handling disabled");
				return;
			}
			Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
			CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
			//所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找資源;
			//webjars:以jar包的方式引入靜態(tài)資源;
			if (!registry.hasMappingForPattern("/webjars/**")) {
				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
			//"/**" 訪問當(dāng)前項(xiàng)目的任何資源,都去(靜態(tài)資源的文件夾)找映射
			String staticPathPattern = this.mvcProperties.getStaticPathPattern();
			//靜態(tài)資源文件夾映射
			if (!registry.hasMappingForPattern(staticPathPattern)) {
				customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
						.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
		}

resourceProperties

			源碼:
			//可以設(shè)置和靜態(tài)資源有關(guān)的參數(shù),緩存時間等
			@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
			public class ResourceProperties {

getStaticPathPattern():

		源碼:			
		private String staticPathPattern = "/**";		
		public String getStaticPathPattern() {
			return this.staticPathPattern;
		}

getStaticLocations()

			源碼:
			private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
			"classpath:/resources/", "classpath:/static/", "classpath:/public/" };			
			private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;			
			public String[] getStaticLocations() {
				return this.staticLocations;
			}

//配置歡迎頁映射

		@Bean
		public WelcomePageHandlerMapping welcomePageHandlerMapping(
				ResourceProperties resourceProperties) {
				//靜態(tài)資源文件夾下的所有index.html頁面;被"/**"映射;
			return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
					this.mvcProperties.getStaticPathPattern());
		}
		private Optional<Resource> getWelcomePage() {
			String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
			return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
		}
		private Resource getIndexHtml(String location) {
			return this.resourceLoader.getResource(location + "index.html");
		}	

getStaticPathPattern()

   源碼:
   private String staticPathPattern = "/**";
   public String getStaticPathPattern() {
    return this.staticPathPattern;
   }

//配置喜歡的圖標(biāo)

  @Configuration
  @ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
  public static class FaviconConfiguration implements ResourceLoaderAware {
   private final ResourceProperties resourceProperties;
   private ResourceLoader resourceLoader;
   public FaviconConfiguration(ResourceProperties resourceProperties) {
    this.resourceProperties = resourceProperties;
   }
   @Override
   public void setResourceLoader(ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
   }
   @Bean
   public SimpleUrlHandlerMapping faviconHandlerMapping() {
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
    //所有  **/favicon.ico 都是在靜態(tài)資源文件下找
    mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
    return mapping;
   }
   @Bean
   public ResourceHttpRequestHandler faviconRequestHandler() {
    ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
    requestHandler.setLocations(resolveFaviconLocations());
    return requestHandler;
   }
   private List<Resource> resolveFaviconLocations() {
    String[] staticLocations = getResourceLocations(this.resourceProperties.getStaticLocations());
    List<Resource> locations = new ArrayList<>(staticLocations.length + 1);
    Arrays.stream(staticLocations).map(this.resourceLoader::getResource).forEach(locations::add);
    locations.add(new ClassPathResource("/"));
    return Collections.unmodifiableList(locations);
   }
  }

Thymeleaf使用

 源碼:
 //只要我們把HTML頁面放在classpath:/templates/,thymeleaf就能自動渲染;
 @ConfigurationProperties(prefix = "spring.thymeleaf")
 public class ThymeleafProperties {
  private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
  public static final String DEFAULT_PREFIX = "classpath:/templates/";
  public static final String DEFAULT_SUFFIX = ".html"; 
 }

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java經(jīng)典排序算法之歸并排序?qū)崿F(xiàn)代碼

    Java經(jīng)典排序算法之歸并排序?qū)崿F(xiàn)代碼

    這篇文章主要介紹了Java經(jīng)典排序算法之歸并排序?qū)崿F(xiàn)代碼,歸并排序是建立在歸并操作上的一種有效的排序算法,該算法是采用分治法的一個非常典型的應(yīng)用,將已有序的子序列合并,得到完全有序的序列,需要的朋友可以參考下
    2023-10-10
  • Java連接PostgreSql數(shù)據(jù)庫及基本使用方式

    Java連接PostgreSql數(shù)據(jù)庫及基本使用方式

    這篇文章主要介紹了Java連接PostgreSql數(shù)據(jù)庫及基本使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 通過Java實(shí)現(xiàn)反向代理集群服務(wù)的平滑分配

    通過Java實(shí)現(xiàn)反向代理集群服務(wù)的平滑分配

    這篇文章主要介紹了如何通過Java語言,自己編寫的平滑加權(quán)輪詢算法,結(jié)合線程池和Socket?網(wǎng)絡(luò)編程等,并實(shí)現(xiàn)反向代理集群服務(wù)的平滑分配,需要的可以參考一下
    2022-04-04
  • 解析spring加載bean流程的方法

    解析spring加載bean流程的方法

    這篇文章主要介紹了解析spring加載bean流程的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • 關(guān)于動態(tài)參數(shù)使用@PathVariable的解析

    關(guān)于動態(tài)參數(shù)使用@PathVariable的解析

    這篇文章主要介紹了關(guān)于動態(tài)參數(shù)使用@PathVariable的解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • java中Vector類的常用方法詳解

    java中Vector類的常用方法詳解

    這篇文章主要為大家詳細(xì)介紹了java中Vector類的常用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • 解決DataOutputStream亂碼的問題

    解決DataOutputStream亂碼的問題

    這篇文章主要介紹了DataOutputStream亂碼問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java使用過濾器防止SQL注入XSS腳本注入的實(shí)現(xiàn)

    Java使用過濾器防止SQL注入XSS腳本注入的實(shí)現(xiàn)

    這篇文章主要介紹了Java使用過濾器防止SQL注入XSS腳本注入,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java中的Set、List、Map的用法與區(qū)別介紹

    Java中的Set、List、Map的用法與區(qū)別介紹

    這篇文章主要介紹了Java中的Set、List、Map的用法與區(qū)別,需要的朋友可以參考下
    2016-06-06
  • Spring Cloud構(gòu)建Eureka應(yīng)用的方法

    Spring Cloud構(gòu)建Eureka應(yīng)用的方法

    這篇文章主要介紹了Spring Cloud構(gòu)建Eureka應(yīng)用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03

最新評論