spring template engine thymeleaf css No mapping for GET 404

投稿者:

Spring + thymeleaf で
CSSやJSを見つけられない場合

Thank you for reading this post, don't forget to subscribe!

カスタムクラスで
resources/static 以下を読めるようにする

@Configuration
public class CustomWebMvcConfigurationSupport extends WebMvcConfigurationSupport {

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

    @Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		if (!registry.hasMappingForPattern("/webjars/**")) {
			registry.addResourceHandler("/webjars/**")
					.addResourceLocations("classpath:/META-INF/resources/webjars/");
		}
		if (!registry.hasMappingForPattern("/**")) {
			registry.addResourceHandler("/**")
					.addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
		}
	}
}