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可以這么配置
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的配置都會(huì)失效,你需要自己去配置每一項(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 > |
引入成功之后,自動(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引入依賴
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)用,不需要寫具體版本號(hào)
<script type="text/javascript" src="${request.contextPath }/webjarslocator/jquery/jquery.js"></script>
5.使用ResourceUrlProvider對(duì)自定義的靜態(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; } } |
前端頁面上,我們這么引入
<script type="text/javascript" src="${request.contextPath }/${urls.getForLookupPath('/js/index.js')}"></script>
而實(shí)際上,在生成的html頁面上,已加上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í)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/yingxiake/article/details/51295551