我使用Thymeleaf作为Java webapp的模板引擎,但是我在渲染模板时遇到了问题。该请求似乎处理正确,但浏览器中的页面为空白。
Thymeleaf发动机配置
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模板时,Thymeleaf功能已被注释掉。
<!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>
答案 0 :(得分:1)
正在使用的进程函数返回带有已解析模板数据的String。
public final String process(String templateName, IContext context)
调用应包含响应编写器,以便将解析后的模板直接写入编写器
public final void process(String templateName, IContext context, Writer writer)
应该花更多时间在Javadocs上。