spring boot 靜態(tài)資源處理方法
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/
<script type="text/javascript" src="${request.contextPath }/js/index.js"></script>
注:若在freemarker獲取request對(duì)象,在spring boot 在application.properties可以這么配置
spring.freemarker.request-context-attribute=request
2.如何自定義靜態(tài)資源映射
spring boot有默認(rèn)的資源映射,如果你覺得有需求需要,需要自己映射資源,可以在application.properties配置資源映射
#資源映射路徑為/content/** spring.mvc.static-path-pattern=/content/** #資源映射地址為classpath:/content/ spring.resources.static-locations=classpath:/content/
配置了之后,默認(rèn)資源映射失效,若要讓默認(rèn)的資源也有效的話,可以基于Java來配置
@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的配置都會(huì)失效,你需要自己去配置每一項(xiàng)
3.配置webjars
webjars能允許我們利用java打包的方式,把web的資源文件打包成jar文件,并利用maven進(jìn)行版本控制http://www.webjars.org/,在pom.xml中jQuery依賴
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>1.11.3</version> </dependency>
引入成功之后,自動(dòng)把資源放到classpath://META-INFO/resources/webjars目錄下,我們可以通過/webjars/** 來訪問
<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è)的去客戶端修改資源版本號(hào),我們利用WebJarAssetLocator來處理,首先在pom.xml引入依賴
<dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator</artifactId> </dependency>
然后定義一個(gè)controller進(jìn)行攔截
@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); } } }
在頁(yè)面上,就這么調(diào)用,不需要寫具體版本號(hào)
<script type="text/javascript" src="${request.contextPath }/webjarslocator/jquery/jquery.js"></script>
5.使用ResourceUrlProvider對(duì)自定義的靜態(tài)資源進(jìn)行管理
在使用第三方庫(kù),我們可以是使用WebJarAssetLocator的方式進(jìn)行版本管理,但是使用自己寫css和js,建議使用ResourceUrlProvider進(jìn)行版本管理,并避免在版本發(fā)生改變時(shí),由于瀏覽器緩存而產(chǎn)生資源版本未改變的錯(cuò)誤
首先我們定義一個(gè)controller將路徑信息推到前端
@ControllerAdvice public class ResourceUrlProviderController { @Autowired private ResourceUrlProvider resourceUrlProvider; @ModelAttribute("urls") public ResourceUrlProvider urls() { return this.resourceUrlProvider; } }
前端頁(yè)面上,我們這么引入
<script type="text/javascript" src="${request.contextPath }/${urls.getForLookupPath('/js/index.js')}"></script>
而實(shí)際上,在生成的html頁(yè)面上,已加上md5的后綴
<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)資源處理
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式
這篇文章主要介紹了Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Elasticsearch 映射參數(shù)詳解 fields
這篇文章主要介紹了fields Elasticsearch 映射參數(shù)fields,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07解決Springboot不能自動(dòng)提交數(shù)據(jù)庫(kù)連接問題
在使用SSM框架開發(fā)時(shí),若在同一Service內(nèi)部方法間互相調(diào)用,直接使用this關(guān)鍵字會(huì)導(dǎo)致事務(wù)管理失效,從而引發(fā)如數(shù)據(jù)庫(kù)連接不足等問題,原因是通過this調(diào)用不會(huì)經(jīng)過Spring的代理,因此不會(huì)自動(dòng)進(jìn)行事務(wù)處理2024-09-09Spring-MVC異步請(qǐng)求之Servlet異步處理
這篇文章主要介紹了Spring-MVC異步請(qǐng)求之Servlet異步處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01Character.UnicodeBlock中cjk的說明詳解
這篇文章主要為大家詳細(xì)介紹了Character.UnicodeBlock中cjk的說明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09