Search code examples
thymeleafspring-thymeleaf

Equivalent of JSP tag libraries for Thymeleaf


I have e.g. a custom JSP tag library files (tagx files) and want to know what would the equivalent for Thymeleaf? I'm using Thymeleaf with Spring.

My label.tagx:

<jsp:root xmlns:c="jakarta.tags.core"
          xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
  <jsp:output omit-xml-declaration="yes"/>

  <jsp:directive.attribute name="required" type="java.lang.Boolean" required="false"
                           description="Indicates if this field is required (default false)"/>
  <jsp:directive.attribute name="render" type="java.lang.Boolean" required="false"
                           description="Indicate if the contents of this tag and all enclosed tags should be rendered (default 'true')"/>

  <c:if test="${empty render or render}">

    <c:if test="${empty required}">
      <c:set var="required" value="${false}"/>
    </c:if>

    <!-- and so on -->

  </c:if>

</jsp:root>

The call within a JSP would be something like: <field:label required="true"/>


Solution

  • Thymeleaf fragments is what I think closest resembles this: https://www.baeldung.com/spring-thymeleaf-fragments