如何在 Thymeleaf 和 Spring Boot 中使用自定义标签库? - IT屋-程序员软件开发技术分享社区
首页
其他开发
如何在 Thymeleaf 和 Spring Boot 中使用自定义标签库?
如何在 Thymeleaf 和 Spring Boot 中使用自定义标签库?
[英] How can I use a custom tag library with Thymeleaf and Spring Boot?
本文介绍了如何在 Thymeleaf 和 Spring Boot 中使用自定义标签库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 Spring MVC、JSP 和 Tyles 创建了一个自定义标记库,因此我有几个 .tagx
文件.在新项目中,我决定尝试 Spring Boot 和 Thymelaf,但我想保留我的自定义库...
那么您是否可以使用 thymleaf 创建自定义标签库?或者我是否可以以任何方式导入我的自定义标签库?
编辑
为了更清楚,我添加了一些代码.以下使用的标签是我自定义的标签.所以我用 xmlns:form="urn:jsptagdir:/WEB-INF/tags/form"
包含在 JSP 中
<div class="row"><div class="col-md-12 text-center"><h1 class="fa fa-user-plus" style="color:green;"><b>  Stai crando un nuovo utente di tipo: <var class="varFont"> ${utente.ruolo}</var></b></h1>
<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 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 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 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 class="col-xs-12 col-sm-12 col-md-4"><field:input field="confermaNuovaPassword" id="c_utente_confirmPassword" required="true" type="password"/>
</表单>
此页面的结果是一个标准的 HTML 页面,其中包含一个表单、一些字段和标签以及一个提交按钮..
这样我就可以快速写出很多html代码了.例如,不是为每个字段写 ..... .....;field:input......>
也使用国际化.
我想在 Thymeleaf 中拥有(并且我认为可能非常有用)同样的东西.
否则,如果您知道使用 Thymeleaf 避免节省代码和时间的方法,请告诉我..
解决方案
我使用以下作为一种解决方案/变通方法.目前我只创建了 2 个简单的标签,我不确定这是否是实现更复杂标签的好方法.
输入标签
<html xmlns:th="http://www.thymeleaf.org"><身体><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="表单控件"/></节>
</html>
显示标签
<html xmlns:th="http://www.thymeleaf.org"><身体><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></节></html>
然后我使用这两个标签传递我想要的参数:
<section th:replace="~{/tags/input :: input(username, *{username}, text, true)}"></section>
和
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..
解决方案
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>
这篇关于如何在 Thymeleaf 和 Spring Boot 中使用自定义标签库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文