spring boot 靜態(tài)資源處理方法
spring boot 秉承約定優(yōu)于配置,spring boot在靜態(tài)資源的處理上就已經(jīng)默認做了處理。
1.默認資源映射
映射”/**”的路徑到 /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對象,在spring boot 在application.properties可以這么配置
spring.freemarker.request-context-attribute=request
2.如何自定義靜態(tài)資源映射
spring boot有默認的資源映射,如果你覺得有需求需要,需要自己映射資源,可以在application.properties配置資源映射
#資源映射路徑為/content/** spring.mvc.static-path-pattern=/content/** #資源映射地址為classpath:/content/ spring.resources.static-locations=classpath:/content/
配置了之后,默認資源映射失效,若要讓默認的資源也有效的話,可以基于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默認關于webmvc的配置都會失效,你需要自己去配置每一項
3.配置webjars
webjars能允許我們利用java打包的方式,把web的資源文件打包成jar文件,并利用maven進行版本控制http://www.webjars.org/,在pom.xml中jQuery依賴
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>1.11.3</version> </dependency>

引入成功之后,自動把資源放到classpath://META-INFO/resources/webjars目錄下,我們可以通過/webjars/** 來訪問
<script type="text/javascript" src="${request.contextPath }/webjars/jquery/1.11.3/jquery.js"></script>
4.webjars資源版本控制
既然引入maven進行版本控制,當有新版本的web資源的時候,當然不希望一個個的去客戶端修改資源版本號,我們利用WebJarAssetLocator來處理,首先在pom.xml引入依賴
<dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator</artifactId> </dependency>
然后定義一個controller進行攔截
@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);
}
}
}
在頁面上,就這么調用,不需要寫具體版本號
<script type="text/javascript" src="${request.contextPath }/webjarslocator/jquery/jquery.js"></script>
5.使用ResourceUrlProvider對自定義的靜態(tài)資源進行管理
在使用第三方庫,我們可以是使用WebJarAssetLocator的方式進行版本管理,但是使用自己寫css和js,建議使用ResourceUrlProvider進行版本管理,并避免在版本發(fā)生改變時,由于瀏覽器緩存而產(chǎn)生資源版本未改變的錯誤
首先我們定義一個controller將路徑信息推到前端
@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>
而實際上,在生成的html頁面上,已加上md5的后綴
<script type="text/javascript" src="/demo//js/index-a789359d91ae435bc45489c5e6978b34.js"></script>
由于ResourceUrlProvider監(jiān)聽了ApplicationListener<ContextRefreshedEvent>
所以在項目refresh的時候,在產(chǎn)生一個新的md5,這樣客戶端的資源路徑就發(fā)生改變,回去服務器重新獲取。
這就是spring boot的靜態(tài)資源處理
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式
這篇文章主要介紹了Mybatis注解方式完成輸入?yún)?shù)為list的SQL語句拼接方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Elasticsearch 映射參數(shù)詳解 fields
這篇文章主要介紹了fields Elasticsearch 映射參數(shù)fields,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
解決Springboot不能自動提交數(shù)據(jù)庫連接問題
在使用SSM框架開發(fā)時,若在同一Service內部方法間互相調用,直接使用this關鍵字會導致事務管理失效,從而引發(fā)如數(shù)據(jù)庫連接不足等問題,原因是通過this調用不會經(jīng)過Spring的代理,因此不會自動進行事務處理2024-09-09
Character.UnicodeBlock中cjk的說明詳解
這篇文章主要為大家詳細介紹了Character.UnicodeBlock中cjk的說明,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09

