我想尝试使用百里香去耦模板逻辑。这在thymeleaf 3.0 documentation中提到。


根据教程,我创建了我的项目here
我的模板和解耦逻辑位于LogicTemplate.htmlLogicTemplate.th.xml文件中。
LogicTemplate.html的代码段如下:




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




 4. LogicTemplate.th.xml的代码片段如下:



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




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


`
但是执行此代码不会产生预期的结果。
实际输出:



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





预期输出:
不得包含此静态文本,而应包含所有产品信息。但是我无法弄清楚该代码段出了什么问题。请帮助我确定代码中的潜在错误。

最佳答案

Thymeleaf默认情况下配置StandardDecoupledTemplateLogicResolver(您正在配置的看起来配置错误)。通过将其更改为以下内容,我可以使您的文件正常工作:

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中有一些错别字,看起来应该像这样:

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

10-07 15:43