防止spring把静态资源识别thymeleaf模板

本人在SpringBoot项目中使用thymeleaf模板时,前端静态资源文件不能被使用的问题,再此记录一下

解决:在拦截器中重写addResourceHandlers方法:

//将templates目录下的CSS、JS文件映射为静态资源,防止Spring把这些资源识别成thymeleaf模版

registry.addResourceHandler("/templates/**.js").addResourceLocations("classpath:/templates/");
registry.addResourceHandler("/templates/**.css").addResourceLocations("classpath:/templates/");
//其他静态资源
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

全部

package com.tz.springbootshiro.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author tz
 * @Classname WebMvcConfig
 * @Description
 * @Date 2019-11-10 10:12
 */
@SpringBootConfiguration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/templates/**.js").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/templates/**.css").addResourceLocations("classpath:/templates/");
//其他静态资源
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

static
在这里插入图片描述

posted @ 2022-07-24 23:38  微信公众号-醉鱼Java  阅读(59)  评论(0)    收藏  举报