1
votes

I am new to Thymeleaf and Spring MVC.

I have been dealing with the following problem: some resources (css or images) don't get loaded by my webpage while other do. They are in the same path and folder, the syntax is the same (i have checked by just switching the name of the resource and it worked).

For example, my Thymeleaf Template can find and read my own css files, but it won't read the bootstrap-4 one.

Here is my project structure:

enter image description here

And here an example of the code trying to read bootstrap.css: enter image description here

The same problem happens with images of the same format.

Any ideas of what could be causing the issue?

Thank you in advance

2
Please don't post code (the thymeleaf template) as images. Those become unreadable.M. Deinum
Did you configure any resourceHandlers by implementing WebMvcConfigurer? If then post resource configuration as well. I guess there must be hardcoding files paths configured.Bala
Have you seen this? stackoverflow.com/a/50196388/600135kol
Are you using spring security in this same project ?Sumit
I am not using spring security within the project. I haven't used any resourceHandler, when should they be used? Doesn't Spring scan the resources folder anyways?user8396526

2 Answers

0
votes

You can register a resource handler by extending a WebMvcConfigurerAdapter.

Something like this.

@EnableWebMvc
@Configuration
public class SpringWebConfig extends WebMvcConfigurerAdapter {
        
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/styles/**").addResourceLocations("/styles/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}

Anyway, the css files should be in the css directory

0
votes

Okay so I understood what the problem was: intelliJ. It didn't see the new files added to the project.

Running mvn clean install fixed the problem