5

I've tried searching for existing answers, but I could not find them.

I'd like to access an ArrayList from an object within an ArrayList, so:

Basically two classes: Glossary and Word. Glossary contains a list with Word objects, the class Word contains a list with more Word objects (related words)

<table>
<span th:each="word : ${glossary.words}">
 <td>
  <tr th:each="relatedWord: ${word.relatedWords}">
    <p th:text="${relatedWord.getName()}"></p>
  </tr>
 <td>
</span>
</table>

Unfortunately this does not work for me..

rwinner
  • 348
  • 3
  • 6
  • 15

2 Answers2

13

I'm not sure but I don't think you can access public non-static getters like you do (assuming getName() is marked as public).

You should try:

<table>
    <span th:each="word : ${glossary.words}">
        <td>
            <tr th:each="relatedWord: ${word.relatedWords}">
                <p th:text="${relatedWord.name}"></p>
            </tr>
        <td>
    </span>
</table>

One note: the above code is absolutely not valid XHTML (span directly inside table, tr directly inside td).

tduchateau
  • 4,351
  • 2
  • 29
  • 40
0

I've trying to figure out this too. Using Spring Boot back-end.

I have a class App with a @ManyToOne relationship with Cube.

I'm trying to produce a table where each record represents an App and within one of the cells would list out the related Cube objects.

<tr th:each="app : ${apps}">
  <td th:text="${app.id}">ID</td>
  <td th:text="${app.name}">Name</td>
  <td th:text="${app.description}">Description</td>
  <td th:text="${app.url}">URL</td>
  <td>
    <ul>
      <li th:each="cube : ${app.cubes}">
        <span th:text="${cube.name}">Name</span>
      </li>
    </ul>
  </td>
</tr>

I currently have 3 Apps in the database, each with 3-4 related Cubes.

The App rows are generating correctly, but the I am only getting a single Cube item listed. As it happens, the id of both objects is 1 I noticed.

Hunter1597
  • 11
  • 2