Search code examples
spring-bootspring-mvcthymeleaf

Thymeleaf - Loop values into textarea


I've been stack with a problem in Thymeleaf, that is, looping values into a textarea. My controller provides me with a list/collection via the model binding:

model.addAttribute("response", getDefectMessage.getMessage());

getDefectMessage.getMessage() returns the following (please see image below), in which I only need description in this case.

enter image description here

In my HTML, I am able to display the index and count of the provided data retrieved from the model (please see sample code below).

<tbody>
    <tr th:each="res, stat : *{response}">
        <td th:text="${stat.count}" />
        <td>
            <textarea type="text" th:text="${res[__${stat.index}__].description}"></textarea>
        </td>
    </tr>
</tbody>

Having said that, I get a SpringEL expression exception when rendering (please see image below).

Exception evaluating SpringEL expression: "res[0].description" (defect_details:24)

Been a newbie in Thymeleaf, may I please get assistance in how I can go about displaying values from a list into a textarea. (Thanks in advance).


Solution

    • res represents a DefectDto. So you access it fields like res.defectDescription
    
        <tbody>
          <tr th:each="res, stat : *{response}">
              <td th:text="${stat.count}" />
              <td>
                  <textarea type="text" th:text="${res.defectDescription}"> 
                  </textarea>
              </td>
           </tr>
        </tbody>
    
    • Or if DefectDto has a method getDescription() then you can do
        <tbody>
          <tr th:each="res, stat : *{response}">
              <td th:text="${stat.count}" />
              <td>
                  <textarea type="text" th:text="${res.description}"> 
                  </textarea>
              </td>
           </tr>
        </tbody>