gpt4 book ai didi

java - 无法实现 Thymeleaf 3.0 解耦逻辑,模板未填充数据?

转载 作者:行者123 更新时间:2023-12-02 02:44:02 25 4
gpt4 key购买 nike

我想尝试使用 thymeleaf 解耦模板逻辑。 thymeleaf 3.0 documentation 中提到了这一点。 .

  1. 根据教程我创建了我的项目 here
  2. 我的模板和解耦逻辑位于 LogicTemplate.html 内& LogicTemplate.th.xml文件。
  3. 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 默认配置一个 StandardDe CoupledTemplateLogicResolver (您正在配置的看起来配置错误)。我可以通过将其更改为以下内容来使您的文件正常工作:

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>

关于java - 无法实现 Thymeleaf 3.0 解耦逻辑,模板未填充数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44940309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com