2012-12-30 266 views
1

我使用Thymeleaf作爲Java webapp的模板引擎,但是我在呈現模板時遇到問題。該請求似乎被正確處理,但瀏覽器中的頁面爲空白。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; 
    } 
} 

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"); 

} 

模板。 Thymeleaf功能在嘗試獲取基本HTML模板時已被註釋掉。

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

回答

1

正在使用的進程函數返回一個字符串與解析的模板數據。

public final String process(String templateName, IContext context) 

呼叫應包括響應作家,這樣的分析模板直接寫入作家

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

本來應該花更多的時間用的Javadoc。