好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot静态资源目录访问

静态资源配置

创建一个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

查看更多关于SpringBoot静态资源目录访问的详细内容...

  阅读:10次