Thymeleaf not finding variable in each

Viewed 34

First time using Thymeleaf and I'm having a bit of trouble. I want to display a table with customers and their orderProducts. My class looks like this,

class GroupOrderCustomer {
    private String customerName;
    private double totalPrice;
    private List<OrderProduct> orderProducts;
}

When inspecting my list of groupOrderCustomers we can see this, enter image description here

I also add groupOrderCustomers to the context of my mail,

Context context = new Context(locale);
context.setVariable("groupOrderCustomers", groupOrderCustomers);

final String groupOrderConfirmationTemplate = "html/groupOrderConfirmation";
String htmlContent = emailTemplateEngine.process(groupOrderConfirmationTemplate, context);
helper.setText(htmlContent, true);

When trying to send the email I get this in my console,

Exception evaluating SpringEL expression: "groupOrderCustomer.orderProducts"
EL1007E: Property or field 'orderProducts' cannot be found on null

This is my email,

<table th:each="groupOrderCustomer : ${groupOrderCustomers}">
  <tr>
    <td th:text="${groupOrderCustomer.customerName}" align="left">
      Martin
    </td>
    <td th:text="${groupOrderCustomer.totalPrice}" align="right">
      100 kr
    </td>
  </tr>
  <td>
    <table th:each="orderProduct : ${groupOrderCustomer.orderProducts}">
      <tr>
        <td th:text="${orderProduct.quantity} + 'st ' + ${orderProduct.name}" align="left">
          1st Kombo
        </td>
        <td th:text="${orderProduct.totalPrice}" align="right">
          100 kr
        </td>
      </tr>
    </table>
  </td>
</table>

When I wrote, th:each="orderProduct : ${groupOrderCustomer.orderProducts}", I expected Thymeleaf to pick-up groupOrderCustomer from the first th:each.

When commenting out the second th:each the email gets sent.

I appreciate all the help I can get for this. Thank you!

1 Answers

The issue was the table structure. After the first closing </tr> tag, I have an <td>, but I should actually have another <tr>.

Broken:

<table th:each="groupOrderCustomer : ${groupOrderCustomers}">
  <tr>
    <td th:text="${groupOrderCustomer.customerName}" align="left">
      Martin
    </td>
    <td th:text="${groupOrderCustomer.totalPrice}" align="right">
      100 kr
    </td>
  </tr>
  <td>
    <table th:each="orderProduct : ${groupOrderCustomer.orderProducts}">
      <tr>
        <td th:text="${orderProduct.quantity} + 'st ' + ${orderProduct.name}" align="left">
          1st Kombo
        </td>
        <td th:text="${orderProduct.totalPrice}" align="right">
          100 kr
        </td>
      </tr>
    </table>
  </td>
</table>

Working:

<table th:each="groupOrderCustomer : ${groupOrderCustomers}">
  <tr>
    <td th:text="${groupOrderCustomer.customerName}" align="left">
      Martin
    </td>
    <td th:text="${groupOrderCustomer.totalPrice}" align="right">
      100 kr
    </td>
  </tr>
  <tr> <!--- Missing --->
    <td>
      <table th:each="orderProduct : ${groupOrderCustomer.orderProducts}">
        <tr>
          <td th:text="${orderProduct.quantity} + 'st ' + ${orderProduct.name}" align="left">
            1st Kombo
          </td>
          <td th:text="${orderProduct.totalPrice}" align="right">
            100 kr
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
Related