SpringBoot项目访问不到static目录下的js文件

404


虽然现在前后端分离了,但有时为了测试方便,还是会直接在项目中,写一个html,如图,某次测试过程中发现html访问正常,但static目录下的文件死活访问不到:
SpringBoot项目访问不到static目录下的js文件_第1张图片

  1. 网上很多做法是因为设置了context-path,结果文件路径没有加上,然后报错404.
  2. 第二种写法是,通过加入配置文件,将static目录的访问进行映射,,此时如果访问static/js/sock.js文件,路径写成js/sock.js就好。
/**
 * @author fang
 * @date 2019/11/2
 **/
@Configuration
public class WebResoucesConfig extends WebMvcConfigurationSupport
{

    @Override
    protected void addResourceHandlers(
    	ResourceHandlerRegistry registry
    ) {
        registry.addResourceHandler("/static/**").
                   addResourceLocations("classpath:/static/");
    }
}
  1. 但还是不行,后面查到如果在pom文件设置资源访问拦截,也可能导致访问不到,才想到我确实做了资源访问的拦截。此时访问sock.js还要加上static变成static/js/sock.js。
<resources>
	<resource>
 		<directory>src/main/resources</directory>
        <includes>
        	<include>application.properties</include>
            <include>**/*.xml
            templates/**
            /*添加了这个*/
            <include>static/**
            
            	
	            	env/**
                
                false
       
       
      	
      		src/main/resources/env/${profiles.active}
      	
	

你可能感兴趣的:(Spring)