Java_User
Java_User

Reputation: 475

How to display List<Map<String, Object>> in HTML 5 using thymeleaf

My application is using HTML5+Thymeleaf I am getting few rows fetched from DB to display as search results.

PersonDAO.java

String sql = "SELECT P.PERSON_ID, P.PERSON_NAME, C.COMPANY_ID FROM PERSON P LEFT JOIN COMPANY C ON P.PERSON_ID = C.PERSON_ID WHERE P.PERSON_ID='1001'";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql );

Output :

[{PERSON_ID=1001, PERSON_NAME=JOHN, COMPANY_ID=10}, {PERSON_ID=1001, PERSON_NAME=JOHN, COMPANY_ID=20}, {PERSON_ID=1001, PERSON_NAME=JOHN, COMPANY_ID=30}, {PERSON_ID=1001, PERSON_NAME=JOHN, COMPANY_ID=40}]

I need to display above values in table as below: (Expected Table in HTML)

PERSON_ID   PERSON_NAME     COMPANY_ID
1001        JOHN            10
1001        JOHN            20
1001        JOHN            30
1001        JOHN            40

For lists, I can do like this:

<div id="resultstab" th:if="!${#lists.isEmpty(searchList)}">    
<tbody>
   <tr th:each="person : ${searchList}">
   <td class="tg bg" th:text="${person.person_id}"></td>
   </tr>
</tbody>
</div>

But, I am not aware how to display for List<Map<String, Object>>. Can anyone help on this. Thanks

Upvotes: 2

Views: 2532

Answers (2)

Prashant E
Prashant E

Reputation: 1

You can try something like this. It will work

<div th:each="map : ${modellist}">
    <div th:each="mapEntry : ${map}">
        <span th:text="${mapEntry.key}"></span> =
        <span th:text="${mapEntry.value}"></span>
    </div>
</div>

Upvotes: 0

Faraj Farook
Faraj Farook

Reputation: 14915

In short

When iterating maps, iter variables will be of class java.util.Map.Entry.

http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#using-theach


In detail

<tbody>
   <tr th:each="person : ${searchList}">
      <td th:each="entry : ${person}" 
          th:text="${entry.value}"></td>
   </tr>
</tbody>

Upvotes: 1

Related Questions