1

Java WebアプリケーションのテンプレートエンジンとしてThymeleafを使用していますが、テンプレートのレンダリングに問題があります。リクエストは正しく処理されているようですが、ブラウザのページが空白です。

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;
    }
}

サーブレットコード

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>
4

1 に答える 1

1

使用されていたプロセス関数は、解析されたテンプレート データを含む文字列を返します。

public final String process(String templateName, IContext context)

解析されたテンプレートがライターに直接書き込まれるように、呼び出しには応答ライターが含まれている必要があります。

public final void process(String templateName, IContext context, Writer writer)

Javadocs にもっと時間を費やすべきでした。

于 2012-12-30T16:24:12.047 に答える