I am having some issues getting my Thymeleaf templates to hot swap / update using Intellij. At the moment I have to do a full server restart in order to see my changes, which is rather tedious and slows down my work flow.
I am using Gradle, Intellij 14.1, and Tomcat 8. I am running the application in Debug mode.
I have tried setting Thymeleaf to not cacheable.
@Configuration
public class ThymeleafConfig {
@Autowired
Environment environment;
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix(environment.getRequiredProperty("thymeleaf.resolver.prefix"));
resolver.setSuffix(environment.getRequiredProperty("thymeleaf.resolver.suffix"));
resolver.setTemplateMode(environment.getRequiredProperty("thymeleaf.resolver.templatemode"));
resolver.setOrder(environment.getRequiredProperty("thymeleaf.resolver.order", Integer.class));
resolver.setCacheable(environment.getRequiredProperty("thymeleaf.resolver.cacheable", Boolean.class));
resolver.setCharacterEncoding(environment.getRequiredProperty("thymeleaf.resolver.character.encoding"));
return resolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
engine.addDialect(new LayoutDialect());
engine.addDialect(new SpringSecurityDialect());
return engine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
return resolver;
}
}
Property file the above code is reading from.
# Thymeleaf
thymeleaf.resolver.prefix=/WEB-INF/views/
thymeleaf.resolver.suffix=.html
thymeleaf.resolver.templatemode=HTML5
thymeleaf.resolver.order=1
thymeleaf.resolver.cacheable=false
thymeleaf.resolver.character.encoding=UTF-8
I also tried setting it in the ApplicationInitializer.
@Override
public void onStartup(ServletContext container) throws ServletException {
/**
* If no active profile is set via -Dspring.profiles.active then the application
* will default to development mode
*/
container.setInitParameter("spring.profiles.default", "dev");
/**
* Set thymeleaf cache to false if -Dspring.thymeleaf.cache is not passed
*/
container.setInitParameter("spring.thymeleaf.cache", "false");
/**
* create the root Spring application context
*/
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.setDisplayName("app");
rootContext.register(AppConfig.class);
/**
* manage the lifecycle of the root application context
*/
container.addListener(new ContextLoaderListener(rootContext));
/**
* register and map the dispatcher servlet
*/
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
So far none of this has worked.
Select exploded war for the deployment. Then you can simply update resources or classes and resources when you hit CMD + F10( I assume it might be CTRL on Windows/Linux).