我使用Thymeleaf作为Java web应用程序的模板引擎,但我在呈现模板时遇到了问题。请求似乎得到了正确的处理,但是浏览器中的页面是空白的。
百里叶发动机配置
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;
}
}
servlet代码
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");
}
模板。当试图获得一个基本的HTML模板来显示时,thyymleaf功能已经被注释掉了。
<!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>
正在使用的进程函数返回一个字符串,其中包含已解析的模板数据。
public final String process(String templateName, IContext context)
调用应该包含响应写入器,以便将解析后的模板直接写入写入器
public final void process(String templateName, IContext context, Writer writer)
应该花更多的时间在Javadocs上。