无法实现Thymeleaf 3.0解耦逻辑,模板没有填充数据?

时间:2017-07-06 05:16:32

标签: java thymeleaf

我想尝试使用百日咳的解耦模板逻辑。这在thymeleaf 3.0 documentation中提到。

  1. 根据教程,我创建了我的项目here
  2. 我的模板&解耦逻辑存在于LogicTemplate.html& LogicTemplate.th.xml档案。
  3. LogicTemplate.html的代码段如下:
  4. 
    
    <html xmlns="http://www.w3.org/1999/xhtml"
    	xmlns:th="http://www.thymeleaf.org">
    <head>
    <title>ThymeLeaf:Example03</title>
    </head>
    <body>
    	<table id="ProductTable">
    		<tr>
    			<td class="Name">Name</td>
    			<td class="Price">Price</td>
    			<td class="InStock">In Stock</td>
    			<td class="Comments">Comments</td>
    		</tr>
    		<tr>
    			<td class="Name">Ppap</td>
    			<td class="Price">10</td>
    			<td class="InStock">Yes</td>
    			<td class="Comments">No Comments</td>
    		</tr>
    	</table>
    </body>
    </html>
    &#13;
    &#13;
    &#13;  4. LogicTemplate.th.xml的代码片段如下:

    &#13;
    &#13;
    <?xml version="1.0"?>
    <thlogic>
      <attr sel="#ProductTable" th:remove="all-but-first">
        <attr sel="/tr[0]" th:each="prod : ${products}">
          <attr sel="td.Name" th:text="${prod.name}" />
          <attr sel="td.Price" th:text="${prod.price}" />
          <attr sel="td.InStock" th:text="${prod.inStock}" />
          <attr sel="td.Comments" th:text="${${prod.comments!=null and (not #lists.isEmpty(prod.comments))}?#lists.size(prod.comments):0}}" />
        </attr>
      </attr>
    </thlogic>
    &#13;
    &#13;
    &#13;   5.我创建了以下java类DeCoupledLogic,代码片段如下所示: `

    public class DeCoupledLogic {
            public static void main(String[] args) {
                final FileTemplateResolver templateResolverFile = new FileTemplateResolver();
                templateResolverFile.setTemplateMode(TemplateMode.HTML);
                templateResolverFile.setPrefix("src/main/resources/templates/html/");
                templateResolverFile.setSuffix(".html");
                templateResolverFile.setCacheTTLMs(1 * 60 * 60 * 1000l);
                templateResolverFile.setCacheable(Boolean.TRUE);
                templateResolverFile.setCharacterEncoding("UTF-8");
                templateResolverFile.setCheckExistence(true);
                templateResolverFile.setUseDecoupledLogic(true);
                templateResolverFile.setCheckExistence(true);
    
                final StandardDecoupledTemplateLogicResolver resolver=new StandardDecoupledTemplateLogicResolver();
                resolver.setPrefix("src/main/resources/templates/html/");
    
                final TemplateEngine templateEngine = new TemplateEngine();
                templateEngine.setTemplateResolver(templateResolverFile);
                templateEngine.setDecoupledTemplateLogicResolver(resolver);
    
                final Context context01 = new Context();
                context01.setVariable("products",ProductRepository.getInstance().findAll());
    
                final BufferedWriter writer01=new BufferedWriter(new OutputStreamWriter(System.out));
                templateEngine.process("LogicTemplate",context01,writer01);     
            }
        }
    

    ` 但是执行此代码并不能提供所需的结果。 输出实际:

    &#13;
    &#13;
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>ThymeLeaf:Example03</title>
    </head>
    <body>
    	<table id="ProductTable">
    		<tr>
    			<td class="Name">Name</td>
    			<td class="Price">Price</td>
    			<td class="InStock">In Stock</td>
    			<td class="Comments">Comments</td>
    		</tr>
    		<tr>
    			<td class="Name">Ppap</td>
    			<td class="Price">10</td>
    			<td class="InStock">Yes</td>
    			<td class="Comments">No Comments</td>
    		</tr>
    	</table>
    </body>
    &#13;
    &#13;
    &#13;

    预期产出: 不得包含此静态文本,但必须包含所有产品信息。但我无法确定此代码块出错的位置。请帮我识别代码中的潜在错误。

1 个答案:

答案 0 :(得分:2)

Thymeleaf默认配置final FileTemplateResolver templateResolverFile = new FileTemplateResolver(); templateResolverFile.setTemplateMode(TemplateMode.HTML); templateResolverFile.setPrefix("src/main/resources/templates/html/"); templateResolverFile.setSuffix(".html"); templateResolverFile.setCacheTTLMs(1 * 60 * 60 * 1000l); templateResolverFile.setCacheable(Boolean.TRUE); templateResolverFile.setCharacterEncoding("UTF-8"); templateResolverFile.setCheckExistence(true); templateResolverFile.setUseDecoupledLogic(true); templateResolverFile.setCheckExistence(true); final TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolverFile); final Context context01 = new Context(); context01.setVariable("products",ProductRepository.getInstance().findAll()); final BufferedWriter writer01=new BufferedWriter(new OutputStreamWriter(System.out)); templateEngine.process("LogicTemplate",context01,writer01); (您正在配置的那个看起来配置错误)。通过将其更改为:

,我能够使您的文件正常工作
<?xml version="1.0"?>
<thlogic>
    <attr sel="#ProductTable" th:remove="all-but-first">
        <attr sel="/tr[0]" th:each="prod : ${products}">
            <attr sel="td.Name" th:text="${prod.name}" />
            <attr sel="td.Price" th:text="${prod.price}" />
            <attr sel="td.InStock" th:text="${prod.inStock}" />
            <attr sel="td.Comments" th:text="${prod.comments != null and (not #lists.isEmpty(prod.comments)) ? #lists.size(prod.comments) : 0}" />
        </attr>
    </attr>
</thlogic>

另外,你的逻辑xml中有一些拼写错误,它应该是这样的:

var message = context.MakeMessage();
message.Speak = "Here is your receipt.";

var attachment = GetReceiptCard();
message.Attachments.Add(attachment);

await context.PostAsync(message);