springboot静态资源无法访问 templates 下面的文件可以访问static 下面的不可以访问springboot 2.0 5C

Request URL: http://localhost:8090/static/css/login.css
Request Method: GET
Status Code: 404
Remote Address: [::1]:8090
Referrer Policy: no-referrer-when-downgrade
Content-Type: application/json;charset=UTF-8
Date: Sat, 20 Jul 2019 07:41:39 GMT
Transfer-Encoding: chunked
X-Application-Context: application:8090
Provisional headers are shown
Referer: http://localhost:8090/login
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537

spring.thymeleaf.mode= HTML5
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.resources.static-locations = classpath:/templates/,/static/
spring.mvc.static-path-pattern= /**

@Configuration
public class BaseInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {

    return true;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {

}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
        throws Exception {

}

}
}

public class WebMvcConfig implements WebMvcConfigurer {

@Resource
private BaseInterceptor baseInterceptor;

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {

}

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

}

@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {

}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

}

@Override
public void addFormatters(FormatterRegistry registry) {

}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(baseInterceptor).excludePathPatterns("/static/**","/templates/**");
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/**").addResourceLocations("clas spath:/staic/");
    registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
}

@Override
public void addCorsMappings(CorsRegistry registry) {

}

@Override
public void addViewControllers(ViewControllerRegistry registry) {

}

@Override
public void configureViewResolvers(ViewResolverRegistry registry) {

}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {

}

@Override
public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {

}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

}

@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {


}

@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {

}

@Override
public void extendHandlerExceptionResolvers(List<HandlerExceptionR      esolver> exceptionResolvers) {

}

@Override
public Validator getValidator() {

    return null;
}

@Override
public MessageCodesResolver getMessageCodesResolver() {

    return null;
}

启动类

@SpringBootApplication
@EnableWebMvc
@MapperScan(basePackages = "com.demo.dao")
public class SpringSecurityApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSecurityApplication.class, args);
}
}

在网上搜到的答案是添加拦截器,有的说static 是默认的静态文件目录,只要放到地下就行了。

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问