靜態(tài)資源配置
創(chuàng)建一個(gè)staticconfig 繼承 webmvcconfigureradapter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.huifer.blog.config; import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.resourcehandlerregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; /** * 描述: * 靜態(tài)文件配置 * @author huifer * @date 2019-01-01 */ @configuration public class staticconfig extends webmvcconfigureradapter { public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler( "/js/**" ).addresourcelocations( "classpath:/static/js/" ); registry.addresourcehandler( "/css/**" ).addresourcelocations( "classpath:/static/css/" ); registry.addresourcehandler( "/fonts/**" ).addresourcelocations( "classpath:/static/fonts/" ); registry.addresourcehandler( "/images/**" ).addresourcelocations( "classpath:/static/images/" ); super .addresourcehandlers(registry); } } |
翻看源碼發(fā)實(shí)現(xiàn)了webmvcconfigurer 故而修改
創(chuàng)建一個(gè)staticconfig 實(shí)現(xiàn) webmvcconfigurer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.huifer.blog.config; import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.resourcehandlerregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigurer; /** * 描述: * 靜態(tài)文件配置 * @author huifer * @date 2019-01-01 */ @configuration public class staticconfig implements webmvcconfigurer { public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler( "/js/**" ).addresourcelocations( "classpath:/static/js/" ); registry.addresourcehandler( "/css/**" ).addresourcelocations( "classpath:/static/css/" ); registry.addresourcehandler( "/fonts/**" ).addresourcelocations( "classpath:/static/fonts/" ); registry.addresourcehandler( "/images/**" ).addresourcelocations( "classpath:/static/images/" ); // super.addresourcehandlers(registry); } } |
修改pom 文件
1
2
3
4
5
6
|
<resources> <resource> <directory>src/main/resources</directory> <filtering> true </filtering> </resource> </resources> |
以上三種方案都可以訪問(wèn)到 static目錄
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/staHuri/article/details/85561243