首页 > 解决方案 > 用百里香添加 css

问题描述

在此处输入图像描述

我想访问 login.css 文件。为此,我尝试在登录页面中执行此操作:

<link rel="stylesheet" type="text/css" href="..static/css/login.css" th:href="@{/static/css/login.css}"/>

我有一个 WebConfig:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");

    }
}

标签: javaspringspring-mvcspring-bootthymeleaf

解决方案


由于您使用 Spring-Boot,因此应自动识别路径。项目结构应该类似:

  • resources/static/js
  • resources/static/css

如果没有其他被覆盖的方法,请删除WebConfig带有注释的类。我建议避免@EnableWebMvc与 Spring-Boot 一起使用,因为您对 MVC 配置拥有所有控制权(和责任)。

通过以下方式访问静态资源:

<link rel="stylesheet" th:href="@{/css/login.css}"/>

推荐阅读