unlimitednzt
unlimitednzt

Reputation: 1065

Spring Boot & Thymeleaf static content not served in war file

I'm building a Spring Boot application with Thymeleaf. My templates (views) and static folders are under src/main/resources/static and src/main/resources/templates. When I run the application via the main method (using eclipse), everything is fine. However, I've followed the instructions to create a war file, and when I deploy it to Tomcat 7 - the static content is missing, only the Thymeleaf html templates are shown.

I've searched all related threads on SO and tried many of the variations - using @EnableWebMVC, removing all of my configuration, changing the static folder name to "public" etc. Nothing seems to work for me, and this looks like a problem with the packaging.

My configuration class:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Autowired
private ThymeleafProperties properties;

/* Disable Thymeleaf auto-caching */
@Bean
public ITemplateResolver defaultTemplateResolver() {

    TemplateResolver resolver = new TemplateResolver();
    resolver.setResourceResolver(thymeleafResourceResolver());
    resolver.setPrefix(this.properties.getPrefix());
    resolver.setSuffix(this.properties.getSuffix());
    resolver.setTemplateMode(this.properties.getMode());
    resolver.setCharacterEncoding(this.properties.getEncoding());
    resolver.setCacheable(false);
    return resolver;
}

@Bean
public SpringResourceResourceResolver thymeleafResourceResolver() {

    return new SpringResourceResourceResolver();
}

}

Class with main method:

@SpringBootApplication
public class RestApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

    return application.sources(RestApplication.class);
}

public static void main(String[] args) {

    SpringApplication.run(RestApplication.class, args);
}

}

Upvotes: 0

Views: 2190

Answers (1)

unlimitednzt
unlimitednzt

Reputation: 1065

Finally found the problem.

My and tags in Themeleaf were starting with a forward slash. e.g - "/css/style.css" instead of "css/style.css".

Hope this helps anyone else who encounters this issue.

Upvotes: 2

Related Questions