Search code examples
javahtmlthymeleaf

Using <a> tag in thymeleaf iteration


I need to display a list of links coming through list using thymeleaf. I was successfully able to do the same using following code:

<div class="col-12 col-md-6" th:each="ApplicationVO: ${ApplicationList}"> 
              <a class="service-link shadow" title="MMIS" href="mmis.html" target="_blank"><span th:text="${ApplicationVO.applicationName} +'&raquo'"></span></a>
          </div>

My question is how do i put the link(href) and title in anchor tag as ApplicationVO.applicationLink and ApplicationVO.displayName?


Solution

  • I guess you need to use th:href tag:

    <div class="col-12 col-md-6" th:each="ApplicationVO: ${ApplicationList}"> 
              <a class="service-link shadow" title="MMIS" th:href="@{${ApplicationVO.applicationLink}}" target="_blank"><span th:text="${ApplicationVO.applicationName} +'&raquo'"></span></a>
    </div>
    

    I think that you could also remove the span tag like this :

    <div class="col-12 col-md-6" th:each="ApplicationVO: ${ApplicationList}"> 
              <a class="service-link shadow" title="MMIS" th:text="${ApplicationVO.applicationName} +'&raquo'" th:href="@{${ApplicationVO.applicationLink}}" target="_blank"></a>
    </div>