I want to try decoupled template logic using thymeleaf. This is mentioned here in thymeleaf 3.0 documentation.
<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>
<?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>
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);
}
}
` But executing this code does not gives desired result. Output Actual:
<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>
Output Expected: Must not contain this static text but all product information.But I am unable to figure out where this code piece went wrong.Please Help me to identify potential error in my code.
Thymeleaf configures a StandardDecoupledTemplateLogicResolver
by default (and the one you are configuring looks like it is configured wrong). I was able to get your files to work by changing it to this:
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);
Also, you have some typos in your logic xml, it should look like this:
<?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>