0

I am working on a spring boot application and trying to convert jsp to thymeleaf. I am stuck converting the following jsp code to thymeleaf: I do not know how to convert the <c:import> part.

<c:forEach items="${requestScope.users}" var="user" varStatus="status">
     <div <c:if test="${!status.last}"></c:if>>
         <c:import url="/user/checkUser">
             <c:param name="username" value="${user.username}" />
             <c:param name="firstName" value="${user.firstName}" />
             <c:param name="lastName" value="${user.lastName}" />
         </c:import>
     </div>
</c:forEach>
Kurt Shaw
  • 609
  • 2
  • 10
  • 26

1 Answers1

2

There is no direct replacement in Thymeleaf. The best way is to create a fragement that has the HTML that lives at the /user/checkUser URL.

For instance, create src/main/resources/templates/fragments.html:

<div th:fragment="show-user-info(username, firstName, lastName)">
  <div th:text="${username}"></div>
  <div th:text="${firstName}"></div>
  <div th:text="${lastName}"></div>
</div>

Now, you can use the fragment:

<div th:each="user : ${users}">
  <th:block th:if="${!status.last}">
    <div th:replace="fragments :: show-user-info(${user.username},${user.firstName},${user.lastName})"></div>
  </th:block>
</div>
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211