关于thymeleaf静态资源引用无效

一. 问题背景

springboot导入thymeleaf依赖后,thymeleaf引入bootstrap静态资源失败,路径已经使用了th:href="@{/...}"

二. 解决方案

原理我在全局配置文件中定义了spring.mvc.servlet.path=/crud,我用这个的目的是定义项目路径,使得访问localhost:8080/crud/才有效。其实应该使用server.servlet.context-path=/crud

如果SpringBoot的版本是2.0及以上,静态资源会被拦截,可以尝试将静态资源放行,如下:

package com.atguigu.springboot.config;

import com.atguigu.springboot.component.LoginHandlerInterceptor;
import com.atguigu.springboot.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
//@EnableWebMvc
public class MyMvcConfig2 implements WebMvcConfigurer {

    /**
     * 所有的WebMvcConfigurer都会一起起作用
     * @return
     */
    @Bean //将组件加载进容器中
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
           
            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                        .excludePathPatterns("/asserts/**", "/webjars/**");
            }
        };
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值