Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 349
William Sousa Avatar asked Oct 18 '22 15:10

William Sousa


1 Answers

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.

like image 198
Ali Dehghani Avatar answered Oct 21 '22 06:10

Ali Dehghani