简体   繁体   中英

How to set the CSS with Spring Boot and Thymeleaf

I've been trying to make my css file work with Spring Boot and Thymeleaf (or even without Thymeleaf) but its not working at all.

I've checked many answers on StackOverFlow, tried everything, but its still not working. Here's a sample of what I've done:

My 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;
}

With thymeleaf, I also tried:

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

My css file is in resources/static/css/index.css (I've tried that since that's what people from stackoverflow suggested), but no luck.

I also have a 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();
    }
}

Only warning I managed to grab from the terminal is this one:

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]

My application.properties:

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

If you ever faced the same issue, I'd love to get help.

Thanks!

EDIT : Found something. I've posted it below.

Seems like the following line in "application.properties" was the problem:

spring.resources.add-mappings=false

Now it's working, I'd love to explain why it fixed the issue, but as I have no idea. I better leave it like that. Still hope it'll help some of you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM