Reputation: 31
I am using Thymeleaf as the template engine for a Java webapp, but I am having a problem with rendering the template. The request appears to be processed correctly, but the page in the browser is blank.
Thymeleaf engine configuration
public static class TemplateEngineProvider implements Provider<TemplateEngine> {
private static final Logger logger = LoggerFactory.getLogger(TemplateEngineProvider.class);
private TemplateEngine templateEngine;
TemplateEngineProvider() {
logger.debug("Initializing template Engine");
TemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
this.templateEngine = new TemplateEngine();
this.templateEngine.setTemplateResolver(templateResolver);
}
@Override
public TemplateEngine get() {
return this.templateEngine;
}
}
The servlet code
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
logger.trace("Loading dashboard");
DBI dbi = dbiProvider.get();
PostDAO postDAO = dbi.onDemand(PostDAO.class);
long postCount = postDAO.getPostCount();
resp.setContentType("text/html");
logger.trace("Calling template engine");
WebContext ctx = new WebContext(req, resp, getServletContext());
ctx.setLocale(req.getLocale());
this.templateEngine.process(TEMPLATE_NAME, ctx);
logger.trace("Done processing request");
}
The template. The Thymeleaf functionality has been commented out while trying to get a basic HTML template to be displayed.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>dashboard</title>
<!--<title>th:text="${page.title}</title>-->
</head>
<body>
Admin dashboard
<!--<div th:substituteby="header::header"></div>
<p th:text="#{dashboard.post.total_count(${post_count})}"></p>
<div th:substituteby="footer::footer"></div>-->
</body>
</html>
Upvotes: 2
Views: 2333
Reputation: 31
The process function that was being used returns a String with the parsed template data.
public final String process(String templateName, IContext context)
The call should have included the responses writer so that the parsed template is written directly to the writer
public final void process(String templateName, IContext context, Writer writer)
Should have spent more time with the Javadocs.
Upvotes: 1