码农网是专业服务站长和IDC商的信息平台,欢迎你浏览!

springboot themaleaf 第一次进页面不加载css的问题

时间:2022-10-27 19:23:57 阅读:3444次 来源:互联网

springboot themaleaf 第一次进页面不加载css

近期在做springboot +themaleaf项目中遇到首页css样式不加载情况,后来发现是注册拦截器时没有加入css样式,下边是最开始代码

public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor( new LoginHandleInterceptor()).addPathPatterns("/**")
               .excludePathPatterns("/index.html",  // 排除掉首页请求
                       "/",              // 排除掉首页请求
                    ) ;
 
       //registry.addInterceptor(new HandlerInterceptor()).
   }

第一次访问登录页面的时候,对应的js css呗拦截器拦截,就没有加载,只需要把对应的css,jquery等放入到拦截器中就可以了

public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor( new LoginHandleInterceptor()).addPathPatterns("/**")
               .excludePathPatterns("/index.html",  // 排除掉首页请求
                       "/",              // 排除掉首页请求
                       "/user/login",  
                       "/asserts/css/*.css",
                       "/asserts/img/*.svg",
                       "/asserts/js/*.js",
                       "/webjars/bootstrap/4.1.1/css/*.css",
                        "/mancenter/*",
                       "/error", "/asserts/lib/jquery/*","/asserts/lib/*.js") ;
 
       //registry.addInterceptor(new HandlerInterceptor()).

   }

springboot themaleaf 各种报错问题

1.访问themaleaf页面报错

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jun 24 11:08:43 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

错误1:调试时加入了WebMvcConfig类

package com.feilong.Reptile.config;
  
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  
/**
 * 配置静态资源映射
 *
 * @author sunziwen
 * @version 1.0
 * @date 2018-11-16 14:57
 **/
@Component
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 添加静态资源文件,外部可以直接访问地址
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

删除这个类后还是报错,猜测可能是包路径问题,重新建立个新项目,将旧项目转移后,没有再报错。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持码农网。

本文地址:https://www.manongw.com/article/426.html

文章来源:转载于CSDN,转载网址为https://blog.csdn.net/jungeCSND/article/details/103486151

版权申明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 ezhongheng@126.com 举报,一经查实,本站将立刻删除。