I created a custom tag library with Spring MVC, JSP, and Tyles, so I have several .tagx
files.
With the new project I decided to try Spring Boot and Thymelaf, but I would like to keep my custom library...
So do you if is possible to create a custom tag library using thymleaf? Or if can I import my custom tag library in any way?
EDIT
I add some piece of code to be more clear. The following used tags are my customized tags. So I included inside the 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>  Stai creando un nuovo utente di tipo: <var class="varFont"> ${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-]+)*\.([a-z]{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>
</form>
The result of this page is a standard HTML page, with a form, some fields and labels inside it and a submit button..
In this way I can write quickly a lot of html codes. For example instead of write <label>..... </label><input....../>
for each fields, I can write only <field:input......>
using also the internationalization.
I would like to have (and I think could be very useful) the same thing in Thymeleaf.
Otherwise, if you know a method using Thymeleaf to avoid to save codes and time, please tell me..
On the other hand, Thymeleaf is a template engine which takes the HTML file, parses it and then produces web content which is being served. Thymeleaf is more like an HTML-ish view when you compare it with JSP views. If we put all this to an edge, thymeleaf is the slow one here.
Thymeleaf Templatehtml in the path src/main/resources/templates. This template displays an HTML form to allow the user to select and upload an image. Furthermore, we use the accept=”image/*” attribute to allow users to import images only instead of importing any kind of files.
If you are building web front-ends with Spring Boot or Spring MVC, and you're still using JSP (Java Server Pages) then this course is for you. Thymeleaf is a great templating engine which replaces JSP, and you can easily use it in any Spring MVC or Spring Boot application.
I used the following as a kind of solution/workaround. At the moment I 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 TAG
<!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 that I want like:
<section th:replace="~{/tags/input :: input(username, *{username}, text, true)}"></section>
and
<section th:replace="~{/tags/display :: display(nome, *{nome})}"></section>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With