springboot静态资源配置

将jquery.js和index.html放在src/main/resource源目录下

输入地址 ,发现找不到资源

http://localhost:8088/jquery.js

1.默认地址

查看源码WebMvcAutoConfiguration.java中的WebMvcAutoConfigurationAdapter静态内部类WebMvcAutoConfigurationAdapter中的addResourceHandlers方法

            @Override
		    public void addResourceHandlers(ResourceHandlerRegistry registry) {
			if (!this.resourceProperties.isAddMappings()) {
				logger.debug("Default resource handling disabled");
				return;
			}
			Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
			CacheControl cacheControl = this.resourceProperties.getCache()
					.getCachecontrol().toHttpCacheControl();
			if (!registry.hasMappingForPattern("/webjars/**")) {
				customizeResourceHandlerRegistration(registry
						.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod))
						.setCacheControl(cacheControl));
			}
			String staticPathPattern = this.mvcProperties.getStaticPathPattern();
			if (!registry.hasMappingForPattern(staticPathPattern)) {
				customizeResourceHandlerRegistration(
						registry.addResourceHandler(staticPathPattern)
								.addResourceLocations(getResourceLocations(
										this.resourceProperties.getStaticLocations()))
								.setCachePeriod(getSeconds(cachePeriod))
								.setCacheControl(cacheControl));
			}
		}

跟踪this.resourceProperties.getStaticLocations()可以看到默认查找的地址

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
			"classpath:/META-INF/resources/", "classpath:/resources/",
			"classpath:/static/", "classpath:/public/" };

将资源放在这四个位置,springboot可以找到.

优先级顺序为:META-INF/resources > resources > static > public 

2.继承WebMvcConfigurerAdapter或者实现WebMvcConfigurer

package com.spring.boot.config.web;

import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableWebMvc
@Configuration
public class mvcConfig extends WebMvcConfigurerAdapter{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/** 访问都映射到classpath:/ 目录下
        //也可配置/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/**").addResourceLocations("classpath:/");
    }
}

如果非必要的情况不建议这样配置,因为这样配置springboot的web自动配置将不生效,其它项需要自己配置

3.application.properties配置

spring.resources.static-locations=classpath:/

可以配合spring.mvc.static-path-pattern使用

spring.resources.static-locations=classpath:/
spring.mvc.static-path-pattern=/static/**

访问路径就变为

http://localhost:8080/static/jquery.js

4.maven编译打包

在pom.xml中配置

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!--targetPath将打包到指定的目录-->
                <targetPath>META-INF/resources</targetPath>
                <!--表示index.html, jquery.js是一个资源-->
                <includes>
                    <include>index.html</include>
                    <include>jquery.js</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <!--排除index.html, jquery.js-->
                <excludes>
                    <exclude>index.html</exclude>
                    <exclude>jquery.js</exclude>
                </excludes>
                <includes>
                    <include>**</include>
                </includes>
            </resource>
        </resources>
    </build>

查看编译后的格式

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值