ThymeleafTemplateEngine thymeleafEngine = ThymeleafTemplateEngine.create();
TemplateHandler templateHandler = TemplateHandler.create(thymeleafEngine, "src/main/webapp/templates","text/html");
router.route("/dynamic/*").handler(templateHandler);
TemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("src/main/webapp/templates");
templateResolver.setTemplateMode("HTML5");
templateResolver.setSuffix(".html");
thymeleafEngine.getThymeleafTemplateEngine().setTemplateResolver(templateResolver);
TemplateHandler templateHandler = TemplateHandler.create(thymeleafEngine);
templateResolver.setCacheable(false);<div th:replace="header:: header"></div>
Hi All,I am new to Vertx and we recently started using Vertx 3.0 for developing a web application. Right now I am running my vertx application as a .jar file using command promptWe are using thymeleaf templates to develop the UI, I am able to render thymeleaf pages through vertx like thisThymeleafTemplateEngine thymeleafEngine = ThymeleafTemplateEngine.create();
TemplateHandler templateHandler = TemplateHandler.create(thymeleafEngine, "src/main/webapp/templates","text/html");
router.route("/dynamic/*").handler(templateHandler);
Here i have provided the path to look for my thymeleaf templates while creating TemplateHandler. However, I am not able to use TemplateResolver for the same purpose. This is how i tried to use it:TemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("src/main/webapp/templates");
templateResolver.setTemplateMode("HTML5");
templateResolver.setSuffix(".html");
thymeleafEngine.getThymeleafTemplateEngine().setTemplateResolver(templateResolver);
TemplateHandler templateHandler = TemplateHandler.create(thymeleafEngine);
With this code it tries to look for the html page i am requesting under "/templates" location which is the default location for TemplateHandler. So looks the TemplateResolver configuration I provide is not getting picked up at all.Apart from setting the template location i also want to turn of caching atleast in the dev environment so that i do not need to restart Vertx for every template change, that can also be achieved using TemplateResolver as far as i knowtemplateResolver.setCacheable(false);
thEngine = new ThymeleafTemplateEngineImpl();
thEngine.setMode(ThymeleafTemplateEngine.DEFAULT_TEMPLATE_MODE);
TemplateEngine te = thEngine.getThymeleafTemplateEngine();
Set<ITemplateResolver> trs = te.getTemplateResolvers();
for (ITemplateResolver tr : trs) {
((TemplateResolver) tr).setCacheable(false);
}
templateHandler = TemplateHandler.create(thEngine);
Another reason for using TemplateResolver is when i use a thymeleaf template with header and footer fragments e.g.<div th:replace="header:: header"></div>I am getting following error in vertx, which means its not able to find "header.html" file at run time though its present in the same location as test.html file which i am trying to render. I believe if we set that path using TemplateResolver this will get resolved.org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.standard.processor.attr.StandardReplaceFragmentAttrProcessor' (src/main/webapp/templates/testdriver/test.html:30).....Caused by: io.vertx.core.VertxException: io.vertx.core.file.FileSystemException: java.nio.file.NoSuchFileException: header
TemplateEngine te = thEngine.getThymeleafTemplateEngine();TemplateHandler templateHandler = TemplateHandler.create(thEngine,"","text/html");