且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

由于错误的MIME类型而无法加载JavaScript文件(Spring Boot 2,Thymeleaf,Bootstrap)

更新时间:2022-04-23 23:30:05

为您的main.js文件尝试此操作,以使百里香素解析上下文:

Try this for your main.js file, so that thymeleaf resolves the context:

<script th:src="@{/js/main.js}"></script>

如果无法立即使用,请将其添加到用于扩展webconfig的任何类中;如果没有该类,则添加该类本身:

And if it doesn't work out of the box, add this to whatever class you use to extend your webconfig, or add the class itself if you don't have one:

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig implements WebMvcConfigurer    {

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

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
            .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
    }
}