When I run my Spring Boot Project with Thymeleaf, the browser won’t load the css files but path for href and th:href is correct

Spring Boot serves anything that is in src/main/resources/static at the root of your application.

So the URL to use to get the CSS with Thymeleaf should be /css/styles.css.

Edit your HTML file like this:

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

In your security configuration, also use /css and not /static/css:

.antMatchers(
                    "/registration**",
                    "/js/**",
                    "/css/**",
                    "/img/**").permitAll()

You can also use:

http.authorizeRequests()
    .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
         .permitAll()
    .antMatchers("/registration**",
                 "/img/**")
        .permitAll()

PathRequest.toStaticResources().atCommonLocations() includes /css/*, /js/*, /images/*, /webjars/* and favicon.ico.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top