William Sousa
William Sousa

Reputation: 83

Static resources using Spring and Thymeleaf

I'm trying to access my static resources on my HTML using the following code:

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

But just works when I put @{static/css/main.css}. I know that when you set the resources folder, you don't need to set the static folder every time when call a static file.

My folder structure:

/webapp
=== /static
==========/css
==========/js
=== /WEB-INF
==========/views

Setting on Spring the mvc configs:

....
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    private TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    private ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".html");
        resolver.setCacheable(false); // On production , turn TRUE
        resolver.setTemplateMode(TemplateMode.HTML);
        return resolver;
    }

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

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

I'm using Spring 4 and Thymeleaf 3 beta. Every css-js-image file that I'm using I need to write 'static' on the path. This way that are coded don't work to use without write the full path. Why?

Upvotes: 5

Views: 8689

Answers (1)

Ali Dehghani
Ali Dehghani

Reputation: 48213

registry.addResourceHandler("/static/**").addResourceLocations("/static/");
                             ^^^^^^^^                           ^^^^^^^
                             ----------- These two are different ------       

Because you're telling spring mvc to serve every request with /static/ path prefix from /static folder in your classpath. So, when you fire a request to static/css/main.css, it will be matched with your resource handler path and will be served successfully.

I know that when you set the resources folder, you don't need to set the static folder everytime when call a static file.

My guess is you're confusing the /static/** path prefix with /static folder name. static in @{static/css/main.css} is referencing to /static/** path prefix you defined in:

registry.addResourceHandler("/static/**")...

not the folder name in:

...addResourceLocations("/static/")

For example, if you define your resource handler like following:

registry.addResourceHandler("/content/**").addResourceLocations("/static/");

Then you should send your request to, say, content/css/main.css.

Update: If you insist to use css/main.css as your path, you should define your resource handler like this:

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

And also put your /static/ folder in src/main/resources.

Upvotes: 3

Related Questions