Search code examples
htmlthymeleaf

Thymeleaf template: concatenate Strings


I have this piece of code, but the option value is not concatenating {item.id}-${driver.id} and instead I got "-2"

<tr th:each="item: ${devices}" >    

   <td class="col_id"   th:text="${item.id}"  ></td><!-- ID -->
   <td class="col_name"     th:text="${item.description}"></td><!-- NAME -->                                      
   <td class="col_name"     th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')}" th:text="${item.application.name}"></td><!-- NAME -->
   <td class="col_name"  >

        <select id="selectAuthorizedDriverId"  >
            <!--  option value="0">Please select the driver</option-->
            <option th:each="driver : ${drivers}" 
                    th:value="${item.id}-${driver.id}" 
                    th:text="${driver.firstName}" 
                    th:selected="${driver.id==item.driverDevices[0].driver.id}">

            </option>
         </select>

Solution

  • Thymeleaf is able to do mathematical operations, in your case:

    th:value="${item.id}-${driver.id}" 
    

    will generate a single result from two integers. Try

    th:value="|${item.id}-${driver.id}|" 
    

    instead as this should make sure the given values are concatenated.