国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - spring boot 靜態(tài)資源處理方法

spring boot 靜態(tài)資源處理方法

2020-08-29 14:46liuxg2013 Java教程

本篇文章主要介紹了spring boot 靜態(tài)資源處理方法。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

spring boot 秉承約定優(yōu)于配置,spring boot在靜態(tài)資源的處理上就已經(jīng)默認(rèn)做了處理。

1.默認(rèn)資源映射

映射”/**”的路徑到 /static (或/public、/resources、/META-INF/resources), ”/webjars/** 映射到 classpath:/META-INF/resources/webjars/

spring boot 靜態(tài)資源處理方法

 

復(fù)制代碼 代碼如下:

<script type="text/javascript" src="${request.contextPath }/js/index.js"></script>

 

 

注:若在freemarker獲取request對象,在spring boot 在application.properties可以這么配置

?
1
spring.freemarker.request-context-attribute=request

2.如何自定義靜態(tài)資源映射

spring boot有默認(rèn)的資源映射,如果你覺得有需求需要,需要自己映射資源,可以在application.properties配置資源映射

?
1
2
3
4
#資源映射路徑為/content/**
spring.mvc.static-path-pattern=/content/**
#資源映射地址為classpath:/content/
spring.resources.static-locations=classpath:/content/

配置了之后,默認(rèn)資源映射失效,若要讓默認(rèn)的資源也有效的話,可以基于Java來配置

?
1
2
3
4
5
6
7
8
9
10
@Configuration
public class MvcConfiguration extends WebMvcConfigurerAdapter {
 
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
   registry.addResourceHandler("/myres/**").addResourceLocations("classpath:/myres/");
    super.addResourceHandlers(registry);
  }
 
}

這里不要用@EnableWebMvc,如果用了@EnableWebMvc,那sping boot默認(rèn)關(guān)于webmvc的配置都會失效,你需要自己去配置每一項(xiàng)

3.配置webjars

webjars能允許我們利用java打包的方式,把web的資源文件打包成jar文件,并利用maven進(jìn)行版本控制http://www.webjars.org/,在pom.xml中jQuery依賴

?
1
2
3
4
5
<dependency>
  <groupId>org.webjars</groupId>
  <artifactId>jquery</artifactId>
  <version>1.11.3</version>
</dependency>

spring boot 靜態(tài)資源處理方法

引入成功之后,自動把資源放到classpath://META-INFO/resources/webjars目錄下,我們可以通過/webjars/** 來訪問

 

復(fù)制代碼 代碼如下:

<script type="text/javascript" src="${request.contextPath }/webjars/jquery/1.11.3/jquery.js"></script>

 

 

4.webjars資源版本控制

既然引入maven進(jìn)行版本控制,當(dāng)有新版本的web資源的時(shí)候,當(dāng)然不希望一個(gè)個(gè)的去客戶端修改資源版本號,我們利用WebJarAssetLocator來處理,首先在pom.xml引入依賴

?
1
2
3
4
<dependency>
  <groupId>org.webjars</groupId>
  <artifactId>webjars-locator</artifactId>
</dependency>

然后定義一個(gè)controller進(jìn)行攔截

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Controller
public class WebJarsController {
 
  private final WebJarAssetLocator assetLocator = new WebJarAssetLocator();
 
  @ResponseBody
  @RequestMapping("/webjarslocator/{webjar}/**")
  public ResponseEntity<?> locateWebjarAsset(@PathVariable String webjar, HttpServletRequest request) {
    try {
      String mvcPrefix = "/webjarslocator/" + webjar + "/"; // This prefix must match the mapping path!
      String mvcPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
      String fullPath = assetLocator.getFullPath(webjar, mvcPath.substring(mvcPrefix.length()));
      return new ResponseEntity<>(new ClassPathResource(fullPath), HttpStatus.OK);
    } catch (Exception e) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
  }
}

在頁面上,就這么調(diào)用,不需要寫具體版本號

 

復(fù)制代碼 代碼如下:

<script type="text/javascript" src="${request.contextPath }/webjarslocator/jquery/jquery.js"></script>

 

5.使用ResourceUrlProvider對自定義的靜態(tài)資源進(jìn)行管理

在使用第三方庫,我們可以是使用WebJarAssetLocator的方式進(jìn)行版本管理,但是使用自己寫css和js,建議使用ResourceUrlProvider進(jìn)行版本管理,并避免在版本發(fā)生改變時(shí),由于瀏覽器緩存而產(chǎn)生資源版本未改變的錯(cuò)誤

首先我們定義一個(gè)controller將路徑信息推到前端

?
1
2
3
4
5
6
7
8
9
10
11
12
@ControllerAdvice
public class ResourceUrlProviderController {
 
  @Autowired
  private ResourceUrlProvider resourceUrlProvider;
 
  @ModelAttribute("urls")
  public ResourceUrlProvider urls() {
    return this.resourceUrlProvider;
  }
 
}

前端頁面上,我們這么引入

 

復(fù)制代碼 代碼如下:

<script type="text/javascript" src="${request.contextPath }/${urls.getForLookupPath('/js/index.js')}"></script>

 

而實(shí)際上,在生成的html頁面上,已加上md5的后綴

 

復(fù)制代碼 代碼如下:

<script type="text/javascript" src="/demo//js/index-a789359d91ae435bc45489c5e6978b34.js"></script>

 

由于ResourceUrlProvider監(jiān)聽了ApplicationListener<ContextRefreshedEvent>
所以在項(xiàng)目refresh的時(shí)候,在產(chǎn)生一個(gè)新的md5,這樣客戶端的資源路徑就發(fā)生改變,回去服務(wù)器重新獲取。

這就是spring boot的靜態(tài)資源處理

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/yingxiake/article/details/51295551

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲视频在线观看 | 麻豆产精国品免费入口 | 欧美日韩久久精品 | 国产一区二区精品 | 国产一区二区免费 | 亚洲一区二区三区在线播放 | 国产精品久久久久久久久久免费 | 亚洲一区二区三区中文字幕 | 一级片欧美| 精品国产乱码久久久久久牛牛 | 欧美一级久久 | 日韩免费在线观看视频 | 色狠狠一区二区三区香蕉 | 日韩国产高清在线 | 欧美成人精品一区 | 日韩成人在线电影 | 91精品国产综合久久婷婷香蕉 | 欧洲成人午夜免费大片 | 少妇精品久久久久久久久久 | 国产一区二区三区播放 | 成人国内精品久久久久一区 | 国产一区二区三区在线免费看 | 亚洲精品成人 | 北条麻妃99精品青青久久 | 91麻豆产精品久久久久久 | 久久综合一区二区三区 | 免费av电影网站 | 在线观看91 | 福利国产 | 国产一级在线免费观看 | 午夜国产精品视频 | 日韩三级在线观看 | 日韩在线 | 激情五月综合网 | 云南一级毛片 | 日韩码有限公司在线观看 | 在线成年人电影 | 欧美 日韩 中文 | 日韩欧美国产一区二区 | 精品综合99久久久久久www | 免费日韩成人 |