spring Boot 默認的處理方式就已經足夠了,默認情況下Spring Boot 使用WebMvcAutoConfiguration
中配置的各種屬性。
建議使用Spring Boot 默認處理方式,需要自己配置的地方可以通過配置文件修改。
但是如果你想完全控制Spring MVC,你可以在@Configuration
注解的配置類上增加@EnableWebMvc
,增加該注解以后WebMvcAutoConfiguration
中配置就不會生效,你需要自己來配置需要的每一項。這種情況下的配置方法建議參考WebMvcAutoConfiguration
類。
本文以下內容針對Spring Boot 默認的處理方式,部分配置通過在application.yml
配置文件中設置。
配置資源映射
Spring Boot 默認配置的/**
映射到/static
(或/public
,/resources
,/META-INF/resources
),/webjars/**
會映射到classpath:/META-INF/resources/webjars/
。
注意:上面的/static
等目錄都是在classpath:
下面。
如果你想增加如/mystatic/**
映射到classpath:/mystatic/
,你可以讓你的配置類繼承WebMvcConfigurerAdapter
,然后重寫如下方法:
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/mystatic/**") .addResourceLocations("classpath:/mystatic/"); }
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
這種方式會在默認的基礎上增加/mystatic/**
映射到classpath:/mystatic/
,不會影響默認的方式,可以同時使用。
靜態資源映射還有一個配置選項,為了簡單這里用.properties
方式書寫:
spring.mvc.static-path-pattern=/** # Path pattern used for static resources.
- 1
- 1
這個配置會影響默認的/**
,例如修改為/static/**
后,只能映射如/static/js/sample.js
這樣的請求(修改前是/js/sample.js
)。這個配置只能寫一個值,不像大多數可以配置多個用逗號隔開的。
使用注意
例如有如下目錄結構:
└─resources
│ application.yml
│
├─static │ ├─css │ │ index.css │ │ │ └─js │ index.js │ └─templates index.ftl
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
在index.ftl
中該如何引用上面的靜態資源呢?
如下寫法:
<link rel="stylesheet" type="text/css" href="/css/index.css"> <script type="text/javascript" src="/js/index.js"></script>
- 1
- 2
- 1
- 2
注意:默認配置的/**
映射到/static
(或/public
,/resources
,/META-INF/resources
)
當請求/css/index.css
的時候,Spring MVC 會在/static/
目錄下面找到。
如果配置為/static/css/index.css
,那么上面配置的幾個目錄下面都沒有/static
目錄,因此會找不到資源文件!
所以寫靜態資源位置的時候,不要帶上映射的目錄名(如/static/
,/public/
,/resources/
,/META-INF/resources/
)!
使用WebJars
WebJars:http://www.webjars.org/
例如使用jQuery
,添加依賴:
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>1.11.3</version> </dependency>
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
然后可以如下使用:
<script type="text/javascript" src="/webjars/jquery/1.11.3/jquery.js"></script>
- 1
- 1
你可能注意到href
中的1.11.3
版本號了,如果僅僅這么使用,那么當我們切換版本號的時候還要手動修改href
,怪麻煩的,我們可以用如下方式解決。
先在pom.xml
中添加依賴:
<dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator</artifactId> </dependency>
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
增加一個WebJarController
:
@Controller public class WebJarController { private final WebJarAssetLocator assetLocator = new WebJarAssetLocator(); @ResponseBody @RequestMapping("/webjarslocator/{webjar}/**") public ResponseEntity locateWebjarAsset(@PathVariable String webjar, HttpServletRequest request) { try { String mvcPrefix = "/webjarslocator/" + webjar + "/"; 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); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
然后使用的時候按照如下方式:
<script type="text/javascript" src="/webjarslocator/jquery/jquery.js"></script>
- 1
- 1
注意:這里不需要在寫版本號了,但是注意寫url的時候,只是在原來url基礎上去掉了版本號,其他的都不能少!
靜態資源版本管理
Spring MVC 提供了靜態資源版本映射的功能。
用途:當我們資源內容發生變化時,由於瀏覽器緩存,用戶本地的靜態資源還是舊的資源,為了防止這種情況導致的問題,我們可能會手動在請求url的時候加個版本號或者其他方式。
版本號如:
<script type="text/javascript" src="/js/sample.js?v=1.0.1"></script>
- 1
- 1
Spring MVC 提供的功能可以很容易的幫助我們解決類似問題。
Spring MVC 有兩種解決方式。
注意:下面的配置方式針對freemarker
模板方式,其他的配置方式可以參考。
資源名-md5 方式
例如:
<link rel="stylesheet" type="text/css" href="/css/index-2b371326aa93ce4b611853a309b69b29.css">
- 1
- 1
Spring 會自動讀取資源md5,然后添加到index.css
的名字后面,因此當資源內容發生變化的時候,文件名發生變化,就會更新本地資源。
配置方式:
在application.properties
中做如下配置:
spring.resources.chain.strategy.content.enabled=true spring.resources.chain.strategy.content.paths=/**
- 1
- 2
- 1
- 2
這樣配置后,所有/**
請求的靜態資源都會被處理為上面例子的樣子。
到這兒還沒完,我們在寫資源url的時候還要特殊處理。
首先增加如下配置:
@ControllerAdvice public class ControllerConfig { @Autowired ResourceUrlProvider resourceUrlProvider; @ModelAttribute("urls") public ResourceUrlProvider urls() { return this.resourceUrlProvider; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
然后在頁面寫的時候用下面的寫法:
<link rel="stylesheet" type="text/css" href="${urls.getForLookupPath('/css/index.css')}">
- 1
- 1
使用urls.getForLookupPath('/css/index.css')
來得到處理后的資源名。
版本號 方式
在application.properties
中做如下配置:
spring.resources.chain.strategy.fixed.enabled=true spring.resources.chain.strategy.fixed.paths=/js/**,/v1.0.0/** spring.resources.chain.strategy.fixed.version=v1.0.0
- 1
- 2
- 3
- 1
- 2
- 3
這里配置需要特別注意,將version
的值配置在paths
中。原因我們在講Spring MVC 處理邏輯的時候說。
在頁面寫的時候,寫法如下:
<script type="text/javascript" src="${urls.getForLookupPath('/js/index.js')}"></script>
- 1
- 1
注意,這里仍然使用了urls.getForLookupPath
,urls
配置方式見上一種方式。
在請求的實際頁面中,會顯示為:
<script type="text/javascript" src="/v1.0.0/js/index.js"></script>
- 1
- 1
可以看到這里的地址是/v1.0.0/js/index.js
。
靜態資源版本管理 處理過程
在Freemarker模板首先會調用urls.getForLookupPath
方法,返回一個/v1.0.0/js/index.js
或/css/index-2b371326aa93ce4b611853a309b69b29.css
。
這時頁面上的內容就是處理后的資源地址。
這之后瀏覽器發起請求。
這里分開說。
第一種md5方式
請求/css/index-2b371326aa93ce4b611853a309b69b29.css
,我們md5配置的paths=/**
,所以Spring MVC 會嘗試url中是否包含-
,如果包含會去掉后面這部分,然后去映射的目錄(如/static/
)查找/css/index.css
文件,如果能找到就返回。
第二種版本方式
請求/v1.0.0/js/index.js
。
如果我們paths
中沒有配置/v1.0.0
,那么上面這個請求地址就不會按版本方式來處理,因此會找不到上面的資源。
如果配置了/v1.0.0
,Spring 就會將/v1.0.0
去掉再去找/js/index.js
,最終會在/static/
下面找到。
本文參考
- http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html
- http://www.webjars.org/documentation
- http://www.mscharhag.com/spring/resource-versioning-with-spring-mvc
- https://spring.io/blog/2014/07/24/spring-framework-4-1-handling-static-web-resources
如果你使用的JSP或者其他模板,你可以參考上面幾個鏈接的內容。
最后
以上是Spring Boot 靜態資源處理的內容,有些不全面的地方或者讀者有更多疑問,可以查看Spring Boot完整文檔或本文參考的內容。
關於Spring Boot更多的內容可以繼續關注本博客。
Spring Boot 系列
由於我博客Spring Boot 系列文章還不夠多,所以暫時不打算創建專欄,如果再多幾篇我就建專欄。
http://blog.csdn.net/isea533/article/details/50412212