Spring Boot学习(二十)之Spring boot中访问静态资源

落日映苍穹つ 2021-10-18 07:26 509阅读 0赞

SpringBoot对静态资源的映射规则

  1. @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
  2. public class ResourceProperties implements ResourceLoaderAware {
  3. //可以设置和静态资源有关的参数,缓存时间等
  4. WebMvcAuotConfiguration
  5. @Override
  6. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  7. if (!this.resourceProperties.isAddMappings()) {
  8. logger.debug("Default resource handling disabled");
  9. return;
  10. }
  11. Integer cachePeriod = this.resourceProperties.getCachePeriod();
  12. if (!registry.hasMappingForPattern("/webjars/**")) {
  13. customizeResourceHandlerRegistration(
  14. registry.addResourceHandler("/webjars/**")
  15. .addResourceLocations(
  16. "classpath:/META‐INF/resources/webjars/")
  17. .setCachePeriod(cachePeriod));
  18. }
  19. String staticPathPattern = this.mvcProperties.getStaticPathPattern();
  20. //静态资源文件夹映射
  21. if (!registry.hasMappingForPattern(staticPathPattern)) {
  22. customizeResourceHandlerRegistration(
  23. registry.addResourceHandler(staticPathPattern)
  24. .addResourceLocations(
  25. this.resourceProperties.getStaticLocations())
  26. .setCachePeriod(cachePeriod));
  27. }
  28. }
  29. //配置欢迎页映射
  30. @Bean
  31. public WelcomePageHandlerMapping welcomePageHandlerMapping(
  32. ResourceProperties resourceProperties) {
  33. return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
  34. this.mvcProperties.getStaticPathPattern());
  35. }
  36. //配置喜欢的图标
  37. @Configuration
  38. @ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
  39. public static class FaviconConfiguration {

1)、所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;

webjars:以jar包的方式引入静态资源;

http://www.webjars.org/
localhost:8080/webjars/jquery/3.3.1/jquery.js

  1. public static class FaviconConfiguration {
  2. private final ResourceProperties resourceProperties;
  3. public FaviconConfiguration(ResourceProperties resourceProperties) {
  4. this.resourceProperties = resourceProperties;
  5. }
  6. @Bean
  7. public SimpleUrlHandlerMapping faviconHandlerMapping() {
  8. SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
  9. mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
  10. //所有 **/favicon.ico
  11. mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
  12. faviconRequestHandler()));
  13. return mapping;
  14. }
  15. @Bean
  16. public ResourceHttpRequestHandler faviconRequestHandler() {
  17. ResourceHttpRequestHandler requestHandler = new
  18. ResourceHttpRequestHandler();
  19. requestHandler
  20. .setLocations(this.resourceProperties.getFaviconLocations());
  21. return requestHandler;
  22. }
  23. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW9rZWppbjUyMQ_size_16_color_FFFFFF_t_70

访问http://localhost:8080/webjars/jquery/3.3.1/jquery.js

  1. <!‐‐引入jquery‐webjar‐‐>在访问的时候只需要写webjars下面资源的名称即可-->
  2. <dependency>
  3. <groupId>org.webjars</groupId>
  4. <artifactId>jquery</artifactId>
  5. <version>3.3.1</version>
  6. </dependency>

结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW9rZWppbjUyMQ_size_16_color_FFFFFF_t_70 1

2)、”/**“ 访问当前项目的任何资源,都去(静态资源的文件夹)找映射

localhost:8080/abc === 去静态资源文件夹里面找abc

3)、欢迎页; 静态资源文件夹下的所有index.html页面;被”/**“映射;

localhost:8080/ 找index页面

4)、所有的 **/favicon.ico 都是在静态资源文件下找;

5)、自定义配置文件访问规则

@Configuration配置去配置我们需要的一些属性

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  6. @Configuration
  7. public class WebMvcConfig extends WebMvcConfigurerAdapter {
  8. private Logger logger = LoggerFactory.getLogger(this.getClass());
  9. @Override
  10. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  11. registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
  12. registry.addResourceHandler("/upload/**").addResourceLocations("classpath:/upload/");
  13. logger.info("配置静态文件!");
  14. }
  15. }

Spring Boot默认提供静态资源目录位置需置于classpath下, 文件名需符合下面规则:

  • /static
  • /public
  • /resources
  • /META-INF/resources

灵光一现想到web项目的默认页面是直接放在webcontent下面, 虽然需要在web.xml配置, 虽然Spring Boot 并没有web.xml但试试又不要钱于是

只要将index.html放在上面规则的目录下, 在访问项目的时候会被默认加载

可以去我的首页查看群,进入2000人的群交流学习

发表评论

表情:
评论列表 (有 0 条评论,509人围观)

还没有评论,来说两句吧...

相关阅读