abc
abc

Reputation: 512

Add css with thymeleaf

enter image description here

I want to have access to the login.css file. For that I try to do this in the login page:

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

And I have a WebConfig:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

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

    }
}

Upvotes: 1

Views: 4816

Answers (1)

Nikolas
Nikolas

Reputation: 44486

Since you use Spring-Boot, the path should be recognized automatically. The project structure should be alike:

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

In case there are no other overridden methods, remove the WebConfig class with the annotations. I recommend avoiding using the @EnableWebMvc with Spring-Boot since you take all the control (and responsibility) over the MVC configuration.

Access to a static resource with:

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

Upvotes: 5

Related Questions