靜態資源配置
創建一個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; /** * 描述: * 靜態文件配置 * @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); } } |
翻看源碼發實現了webmvcconfigurer 故而修改
創建一個staticconfig 實現 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; /** * 描述: * 靜態文件配置 * @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> |
以上三種方案都可以訪問到 static目錄
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/staHuri/article/details/85561243