Cristian
Cristian

Reputation: 1670

Cannot import static resources in spring 5.0.6 via addResourceHandler and thymeleaf

I have the following folder structure: 'resources' > 'static' > 'styles';
Inside the 'styles' folder there is the file style.css

There is also this class:

@Configuration
@EnableWebMvc
public class ResourcesConfiguration implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/styles/**")
                .addResourceLocations("/styles/", "classpath:/static/");
    }
}  

And in index.html, in the head section there is:

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

I have no other configuration (like in application.properties) regarding static resources.

When I visit index.html in the browser I get:
Request URL:http://localhost:9000/styles/style.css
Request Method:GET
Status Code:404

Upvotes: 1

Views: 100

Answers (1)

narayan-sambireddy
narayan-sambireddy

Reputation: 335

addResourceLocations(String...locations) would take in different locations as a var-arg list. But it does work like prefix or suffix to the adjacent entries.

So you should slightly change your configuration as below to get it working.

    registry
        .addResourceHandler("/styles/**") 
        .addResourceLocations("classpath:/static/styles/"); 

Upvotes: 1

Related Questions