springboot项目访问不了Webapp下面的js和css资源
程序员文章站
2022-06-12 17:30:41
...
写一个配置类:
package com.tmall.tmallspringboot.config;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebMvc
@Configuration
public class CORSConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/");
}
}
17行为将"/**" 映射到target中的"classpath:/META-INF/resources/"。
注意"classpath:/META-INF/resources/"中最后的/不能丢
综上,就可以找到了!