Using variable inside src image with Thymleaf template - spring-boot

I developped an application using spring-boot and thymeleaf as a template
in my view I try to use a variable inside a loop but it's not worked. This is a snippet of my code:
<table >
<thead>
<tr>
<th>Type</th>
<th>Résumé</th>
<th>Contenu</th>
</tr>
</thead>
<tbody>
<tr th:each="subTask : ${lstOtherSubTasks}">
<td><img th:src="#{/img/icons/${subTask.issueTypeId}.png}" title="TODO" /> // here the variable ${subTask.issueTypeId} not works
<p th:text="${subTask.issueTypeId}" /> here the value of the variable ${subTask.issueTypeId} is not null I get the good value
</td>
<td th:text="${subTask.resume}"></td>
<td th:text="${subTask.contenu}"></td>
</tr>
</tbody>
</table>

You can't mix expressions and strings like you're doing. This works:
<img th:src="#{${'/img/icons/' + subTask.issueTypeId + '.png'}}" title="TODO" />

Related

How to remove tag span, space, new line character in Thymeleaf conditional code block?

I am using Spring Boot 2.1.5.RELEASE, Thymeleaf 3.0.11.RELEASE (thymeleaf-3.0.11.RELEASE.jar), Thymeleaf Spring 3.0.11.RELEASE (thymeleaf-spring5-3.0.11.RELEASE.jar)
code snippet
<tr th:each="employee : ${employeeList}">
<td th:text="${employee.order}"></td>
<td th:text="${employee.age}"></td>
<td th:switch="${employee.level}">
<span th:case="0">Junior</span>
<span th:case="1">Senior</span>
<span th:case="2">Expert</span>
</td>
<td th:text="${employee.activeStatus}"></td>
</tr>
For sorting in data-grid by JavaScript, I need remove <span></span> tag pairs. For example, if an employee has level = 2. This time, Thymeleaf will render
<td><span>Expert<span></td>
I hope it become to
<td>Expert</td>
(without <span> tag). How to to this?
I also try
<tr th:each="employee : ${employeeList}">
<td th:text="${employee.order}"></td>
<td th:text="${employee.age}"></td>
<td th:switch="${employee.level}">
<th:block th:case="'0'">Junior</th:block>
<th:block th:case="'1'">Senior</th:block>
<th:block th:case="'2'">Expert</th:block>
</td>
<td th:text="${employee.activeStatus}"></td>
</tr>
But It generate unwanted character
<td>
Expert
</td>
(Has new line or space before text, It is not wanted result)
The answer is, you shouldn't care about the extra spaces & newlines. HTML ignores those spaces when rendering, and so it shouldn't matter to you -- and there is no way to get rid of them short of formatting hacks.
If you really want to get rid of the spaces you could:
Put the switch on one line.
<tr th:each="employee : ${employeeList}">
<td th:text="${employee.order}"></td>
<td th:text="${employee.age}"></td>
<td th:switch="${employee.level}"><th:block th:case="'0'">Junior</th:block><th:block th:case="'1'">Senior</th:block><th:block th:case="'2'">Expert</th:block></td>
<td th:text="${employee.activeStatus}"></td>
</tr>
Create a Map<String, String> levelToDescription, put it on the model, and use that instead of a switch statement.
<tr th:each="employee : ${employeeList}">
<td th:text="${employee.order}" />
<td th:text="${employee.age}" />
<td th:text="${levelToDescription[employee.level]}" />
<td th:text="${employee.activeStatus}" />
</tr>
Etc...
Replace
<td th:switch="${employee.level}">
<th:block th:case="'0'">Junior</th:block>
<th:block th:case="'1'">Senior</th:block>
<th:block th:case="'2'">Expert</th:block>
</td>
by
<th:block th:if="${employee.level} eq '0'"><td>Junior</td></th:block>
<th:block th:if="${employee.level} eq '1'"><td>Senior</td></th:block>
<th:block th:if="${employee.level} eq '2'"><td>Expert</td></th:block>

Why Xpath 3.0 works, but Xquery 3.0 doesn't work with the same expression

I launched Xpath in Oxygen. In Xpath 3.0 found what i need but in Xquery 3.0 doesn't find.
This is my Xpath expression
//table[tbody/tr/th/p[contains(text(), 'All Water System Contacts')]]/tbody/tr[3]/td[1]
This is my xml code
I put part code.
<table border="1" cellpadding="1" cellspacing="1" summary="." width="640">
<tbody>
<tr>
<th colspan="3">
<p>All Water System Contacts </p></th>
</tr>
<tr>
<th>Type</th>
<th>Contact</th>
<th>Communication</th>
</tr>
<tr>
<td align="center">AC - Administrative Contact - GENERAL MANAGER </td>
<td align="center">GRANT, JOHN, W <br/> PO BOX 869<br/> BIG SPRING, TX 79721-0869 </td>
<td align="center">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse"
width="100%">
<tbody>
<tr>
<th><b>Electronic Type</b></th>
<th><b>Value</b></th>
</tr>
</tbody>
</table>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse"
width="100%">
<tbody>
<tr>
<th><b>Phone Type</b></th>
<th><b>Value</b></th>
</tr>
<tr>
<td align="center">BUS - Business</td>
<td align="center">432-267-6341 </td>
</tr>
<tr>
<td align="center">FAX - Facsimile</td>
<td align="center">432-267-3121 </td>
</tr>
<tr>
<td align="center">BUS - Business</td>
<td align="center">432-267-6070 </td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="center">OW - Owner </td>
<td align="center">COLORADO RIVER MUNICIPAL WATER DISTRICT <br/> PO BOX 869<br/> BIG
SPRING, TX 79721-0869 </td>
<td align="center"> </td>
</tr>
</tbody>
</table>
I tried different functions.
I don't know why it doesn't work and what difference
Please help me.
I suspect your real, complete input has an XHTML default namespace declaration xmlns="http://www.w3.org/1999/xhtml" and in oXygen for XPath you have the setting enabled to "use the default namespace of the root element" so your path works with XPath out of the box while for XQuery you need to make sure you explicitly set
declare default element namespace 'http://www.w3.org/1999/xhtml';
in the prolog of your XQuery file or code sample.

How to get TD text by TH name?

I'm using scrapy to parse a doc, this code is like my code structure that I want to parse, I want to get TD text by TH name, How ?
<div id="m_PanelField">
<table width="100%" cellspacing="1" border="0">
<tbody>
<tr>
<th>Etat</th>
<td>XyXy</td>
</tr>
<tr>
<th>IP</th>
<td>XX.XX.XX.XX</td>
</tr>
<tr>
<th>Alias</th>
<td>lorem ipsum</td>
</tr>
</tbody>
</table>
</div>
//td[../th='Etat'] OR .//tr[th='Etat']/td
Thank's for #helderdarocha and #paul-trmbrth

Combine th:each with templating element using Thymeleaf

I have a list of 'product' which I want to show as a list of row table using an html template.
The html template looks like:
<tr th:fragment="productTemplate">
<td th:text="${productName}">product name</td>
<td th:text="${productprice}>product price</td>
</tr>
Here is what I did:
<table>
<tr th:each="product : ${products}" th:substituteby="product :: productTemplate" th:with="productName=*{name}, productPrice=*{price}" />
</table>
If I use th:include, there will be tr nested to each tr
If I use th:substituteby, substitute has the priority on th:each
I cant find a way to replace my loop items by an other.
Somebody have a solution to do this?
I got it:
<table>
<tr th:each="product : ${products}" th:include="product :: productTemplate"
th:with="productName=${product.name}, productPrice=${product.price}"
th:remove="tag" />
</table>
And here, we can keep the template class on the tr element (that what I wanted)
<tbody th:fragment="productTemplate">
<tr class="my-class">
<td th:text="${productName}">product name</td>
<td th:text="${productPrice}">product price</td>
</tr>
</tbody>
here's the result:
<table>
<tr class="my-class">
<td>Lettuce</td>
<td>12.0</td>
</tr>
<tr class="my-class">
<td>Apricot</td>
<td>8.0</td>
</tr>
</table>
thanks to danielfernandez from the official thymeleaf forum
th:include is what you are looking for. The code below works for me. I prefer to put multiple fragments in one file so I've included that here.
<table>
<tr th:each="product : ${products}" th:include="/fragments/productFragment :: productRow" />
</table>
...
/fragments/productFragment.html
...
<tr th:fragment="productRow">
<td th:text="${product.productName}">product name</td>
<td th:text="${product.productPrice}">product price</td>
</tr>
...

Page.GetRouteUrl in LayoutTemplate of listview?

I am tried to create table structure with header,body,footer in listview which works fine.
But in footer which is in layouttemplate, i tried to add below code which gives error.
<LayoutTemplate>
<table class="sampletable" cellpadding="0" cellspacing="0">
<thead class="tableheader">
<tr>
<th>
<a>Samples </a>
</th>
</tr>
</thead>
<tbody class="tablebody">
<tr id="itemplaceHolder" runat="server">
</tr>
</tbody>
<tfoot class="tablefooter">
<tr>
<td>
<a href='<%:Page.GetRouteUrl("samplelist",null) %>'>more sample</a>
</td>
</tr>
</tfoot>
</table>
</LayoutTemplate>
Is it not allowed to place in layouttemplate?
The error is
"The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)."
Use the 'RouteUrlExpressionBuilder'.
Link
Properly documented at MSDN.

Resources