【问题标题】:How can I use a custom tag library with Thymeleaf and Spring Boot?如何在 Thymeleaf 和 Spring Boot 中使用自定义标签库?
【发布时间】:2018-04-06 11:40:59
【问题描述】:

我用 Spring MVC、JSP 和 Tyles 创建了一个自定义标签库,所以我有几个 .tagx 文件。 在新项目中,我决定尝试 Spring Boot 和 Thymelaf,但我想保留我的自定义库...

那么您是否可以使用 thymleaf 创建自定义标签库?或者我是否可以以任何方式导入我的自定义标签库?

编辑

为了更清楚,我添加了一些代码。以下使用的标签是我的自定义标签。所以我用xmlns:form="urn:jsptagdir:/WEB-INF/tags/form"包含在JSP中

<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-]+)*\.([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>

这个页面的结果是一个标准的 HTML 页面,里面有一个表单、一些字段和标签以及一个提交按钮。..

这样我可以快速写出很多html代码。例如,不用为每个字段写&lt;label&gt;..... &lt;/label&gt;&lt;input....../&gt;,我也可以使用国际化只写&lt;field:input......&gt;

我希望在 Thymeleaf 中拥有(并且我认为可能非常有用)同样的东西。

否则,如果您知道使用 Thymeleaf 避免节省代码和时间的方法,请告诉我..

【问题讨论】:

    标签: spring spring-boot thymeleaf taglib tagx


    【解决方案1】:

    我使用以下作为一种解决方案/解决方法。目前我只创建了 2 个简单的标签,我不确定这是否是实现更复杂标签的好方法。

    输入标签

    <!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>
    

    显示标签

    <!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>
    

    然后我使用这两个标签传递我想要的参数:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2016-03-31
      • 2020-09-14
      • 2015-11-04
      • 2017-06-01
      • 2020-08-22
      • 2018-02-17
      相关资源
      最近更新 更多