How can I use a custom tag library with Thymeleaf and Spring Boot?

I created a custom tag library with Spring MVC, JSP and Tyles, so I have several .tagx files. With a new project, I decided to try Spring Boot and Thymelaf, but I would like to keep my own library ...

So, if you can create your own tag library using thymleaf? Or if I can somehow import my own tag library?

EDIT

I will add some code to be more clear. The following tags used are my customized tags. So I turned on JSP with xmlns:form="urn:jsptagdir:/WEB-INF/tags/form"

 <form:create id="fu_utente" modelAttribute="utente" path="/utente" > <div class="row"> <div class="col-md-12 text-center"> <h1 class="fa fa-user-plus" style="color:green;"><b>&#160;&#160;Stai creando un nuovo utente di tipo: <var class="varFont">&#160;${utente.ruolo}</var></b></h1> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-4 col-md-offset-2"> <field:input field="nome" id="c_utente_nome" required="true"/> </div> <div class="col-xs-12 col-sm-12 col-md-4"> <field:input field="userName" id="c_utente_username" min="5" max="15" required="true"/> </div> <div class="col-xs-12 col-sm-12 col-md-8 col-md-offset-2"> <field:input field="email" id="c_Utente_email" required="true" validationRegex="^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([az]{2,4})$"/> </div> <div class="col-xs-12 col-sm-12 col-md-4 col-md-offset-2"> <field:input field="nuovaPassword" id="c_utente_password" min="6" max="15" required="true" type="password"/> </div> <div class="col-xs-12 col-sm-12 col-md-4"> <field:input field="confermaNuovaPassword" id="c_utente_confirmPassword" required="true" type="password"/> </div> </div> </div> </form> 

The result of this page is a standard HTML page with a form, fields and captions inside and a submit button.

This way I can quickly write many html codes. For example, instead of writing <label>..... </label><input....../> for each field, I can only write <field:input......> , using also internationalization.

I would like to have (and, I think, it can be very useful) the same thing in Thimeleaf.

Otherwise, if you know a method using Thymeleaf to avoid saving codes and time, please tell me.

+7
spring spring-boot taglib thymeleaf
source share
1 answer

I used the following as a kind of solution / workaround. At the moment I have created only 2 simple tags, I'm not sure if this is a good way to implement more complex tags.

INPUT TAG

 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <body> <section th:fragment="input(name, value, type, required)" class="form-group" th:switch="${required}"> <label th:text="#{${name}}" th:for="${name}" class="control-label"></label> <input th:case="true" th:type="${type}" th:id="${name}" th:name="${name}" th:value="${value}" class="form-control" required="required"/> <input th:case="false" th:type="${type}" th:id="${name}" th:name="${name}" th:value="${value}" class="form-control"/> </section> </body> </html> 

DISPLAY TYPE

 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <body> <section th:fragment="display(name, value)" class="form-group"> <label th:text="#{${name}}" th:for="${name}" class="control-label"></label> <div class="box" th:id="${name}" th:text="${value}"></div> </section> </body> </html> 

Then I used these 2 tags, passing the parameters I needed:

 <section th:replace="~{/tags/input :: input(username, *{username}, text, true)}"></section> 

and

 <section th:replace="~{/tags/display :: display(nome, *{nome})}"></section> 
+2
source share

All Articles