我在使用Intellij将我的Thymeleaf模板进行热插拔/更新时遇到了一些问题。目前,我必须进行完整的服务器重启才能看到我的更改,这相当乏味,并且减慢了我的工作流程。
我正在使用Gradle、Intellij 14.1和Tomcat 8。我正在调试模式下运行应用程序。
我尝试将百里香叶设置为不可缓存。
@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;
}
}
正在读取上述代码的属性文件。
# 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
我还尝试在应用程序初始值设定项中设置它。
@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("/");
}
}
到目前为止,这些都没有奏效。
为部署选择爆炸战争。然后,当您点击CMD F10时,您可以简单地更新资源或类和资源(我假设它可能是Windows/Linux上的CTRL)。