'Convert jsp <c: import> code to thymeleaf
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>
Solution 1:[1]
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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wim Deblauwe |
