Search code examples
springtaglibthymeleaf

Using custom tags with thymeleaf


If I add some custom tags in my spring-boot project, is it possible use them inside a thymeleaf-based file? For instance, if I had this tag defined in my TLD file (place in the directory /src/main/resources/static):

  <tag>
    <name>Input</name>
    <tag-class>org.store.custom.taglib.form_control.InputTag</tag-class>
  </tag>

Do I could use this in my view like that:

<p th:case="'Input'"><f:Input/></p>

Anyone knows if this is possible?

UPDATE

I try that, but when I run the application, the custom tags are not processed into the corresponding tags:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="/form.tld" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
  <title>cadastro</title>
</head>
<body>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title">cadastro</h3>
    </div>
    <div class="panel-body">
      <f:form th:attr="action=@{/__${command.getClass().getSimpleName()}__/cadastra}">
        <div th:each="item : ${command['class'].declaredFields}">
          <div th:each="item2 : ${item.declaredAnnotations}">
            <div th:switch="${item2.annotationType().simpleName}" th:with="index=${itemStat.index}">
              <p th:case="'Checkbox'"><f:Checkbox/></p>
              <p th:case="'DataList'"><f:DataList/></p>
              <p th:case="'Input'"><f:Input/></p>
              <p th:case="'Radiobutton'"><f:Radiobutton/></p>
              <p th:case="'Select'"><f:Select/></p>
              <p th:case="'Textarea'"><f:Textarea/></p>
            </div>
          </div>
        </div>
      </f:form>
    </div>
  </div>
</body>
</html>

Solution

  • No. Thymeleaf's fundamental principle is that no alien tags should exist. (Only standard html). All dynamic content is added via custom attributes.