【问题标题】:css file is not recognized in Spring在 Spring 中无法识别 css 文件
【发布时间】:2021-12-29 23:04:30
【问题描述】:

我正在使用 Spring、Spring MVC、Thymeleaf 开发一个站点并遇到了问题:css 文件的样式未应用于我的页面。 项目结构如下:

java
   com
      tournament
         config
            MySpringMVCDispatcherServletInitialize.java
            SpConfig.java
         controllers
            HomePageController.java
webapp
   WEB-INF
      views
         css
            style.css
         home_page.html

style.css:

p{
   font-size: 200%;
   font-family: Verdana, Arial, Helvetica, sans-serif;
   color: #336;
}
body{
   background-color: red;
}

home_page.html:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
                xmlns:th="http://thymeleaf.org">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}" media="all">
</head>
<body>
   <p>Hello</p>
</body>
</html>

在 SpConfig 中有这样的配置:

@Configuration
@ComponentScan("com.tournament")
@EnableWebMvc
public class SpConfig implements WebMvcConfigurer {

    private final ApplicationContext applicationContext;

    @Autowired
    public SpringConfig(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        registry.viewResolver(resolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry {
        registry.addResourceHandler("/css/**")
                .addResourceLocations("/WEB-INF/css/");
}

在 home_page.html 中将光标放在th:href="@{/css/style.css}" 上,看起来 css 文件的路径是正确的,但是在页面 home_page.html 上启动服务器时,没有包含在 style.css 中的样式。那么我该如何应用这种风格呢?

【问题讨论】:

  • 你在使用 Spring Boot 吗?如果是这样,您可以完全删除您的 SpConfig 文件并将您的 CSS 放在 src/main/resources/static/css 中,并将您的 Thymeleaf 模板放在 src/main/resources/templates 中,事情就会这样工作。
  • @WimDeblauwe 感谢您的回答,但没有 Spring Boot。

标签: java html spring spring-mvc thymeleaf


【解决方案1】:

你的目录结构显示css在WEB-INF/views/css中,但是你在addResourceLocations中配置了/WEB-INF/css/。根据https://www.baeldung.com/spring-mvc-static-resources,如果你不在那个字符串中使用classpath:前缀,那么路径是相对于webapp/resources的。

因此,或者您需要移动 CSS,或者您需要更新 .addResourceLocations("/WEB-INF/css/") 语句。

【讨论】:

猜你喜欢
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 2011-03-12
  • 2021-01-13
相关资源
最近更新 更多