【问题标题】:How to set the CSS with Spring Boot and Thymeleaf如何使用 Spring Boot 和 Thymeleaf 设置 CSS
【发布时间】:2020-11-11 19:21:17
【问题描述】:

我一直在尝试使我的 css 文件与 Spring Boot 和 Thymeleaf(甚至没有 Thymeleaf)一起工作,但它根本不起作用。

我在 StackOverFlow 上检查了很多答案,尝试了所有方法,但仍然无法正常工作。这是我所做的一个示例:

我的 test.html :

<!doctype>
<html>
<head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
</head>
<body>
    <div>
        <p class="test">trololo</p>
    </div>
</body>
</html>

index.css:

* {
    margin: 0;
    padding: 0;
}

.test {
    color: red;
}

百里香,我也试过:

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

我的 css 文件位于 resources/static/css/index.css 中(我已经尝试过了,因为这是来自 stackoverflow 的人建议的),但没有运气。

我也有一个 SecurityController :

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/**").permitAll();
    }
}

我设法从终端获取的唯一警告是这个:

2020-07-22 05:19:51.148  WARN 42426 --- [nio-8080-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.servlet.NoHandlerFoundException: No handler found for GET /css/index.css]
2020-07-22 05:19:51.166  WARN 42426 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound             : No mapping for GET /favicon.ico
2020-07-22 05:19:51.166  WARN 42426 --- [nio-8080-exec-3] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.servlet.NoHandlerFoundException: No handler found for GET /favicon.ico]

我的 application.properties :

spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

如果您遇到同样的问题,我很乐意寻求帮助。

谢谢!

编辑:找到了一些东西。我已经在下面发布了。

【问题讨论】:

  • 您应该将您的解决方案作为答案发布,而不是将答案留在问题中。这将帮助其他人在未来找到您的解决方案。 (也许您还可以添加一句话,解释为什么它可以解决问题。)
  • @andrewjames :是的,你是对的,我应该这样做,对不起!至于解释为什么它解决了这个问题,我完全不知道,我不想误导人们。

标签: java html css spring-boot thymeleaf


【解决方案1】:

“application.properties”中的以下行似乎是问题所在:

spring.resources.add-mappings=false

现在它开始工作了!我很想解释为什么它解决了这个问题,但由于我不知道,我最好保持这样。仍然希望它对你们中的一些人有所帮助!

【讨论】:

    猜你喜欢
    • 2020-08-16
    • 2017-05-26
    • 2017-09-30
    • 1970-01-01
    • 2016-09-05
    • 2020-10-10
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多