spring boot 秉承約定優於配置,spring boot在靜態資源的處理上就已經默認做了處理。
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.如何自定義靜態資源映射
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對自定義的靜態資源進行管理
在使用第三方庫,我們可以是使用WebJarAssetLocator的方式進行版本管理,但是使用自己寫css和js,建議使用ResourceUrlProvider進行版本管理,並避免在版本發生改變時,由於瀏覽器緩存而產生資源版本未改變的錯誤
首先我們定義一個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監聽了ApplicationListener<ContextRefreshedEvent>
所以在項目refresh的時候,在產生一個新的md5,這樣客戶端的資源路徑就發生改變,回去服務器重新獲取。
這就是spring boot的靜態資源處理
快速系統學習web前端知識到知海匠庫http://www.zhihaijiangku.com