当前位置: 首页 > 知识库问答 >
问题:

我如何使用自定义标签库与Thymeleaf和Spring Boot?

闻人嘉颖
2023-03-14
<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代码。例如,不写 对于每个字段,我只能使用国际化编写

我想在Thymeleaf中有(我认为可能非常有用)同样的东西。

共有1个答案

楚宏胜
2023-03-14

我使用以下方法作为一种解决方案/变通方法。目前我只创建了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>
 类似资料:
  • 本文向大家介绍SpringBoot使用Thymeleaf自定义标签的实例代码,包括了SpringBoot使用Thymeleaf自定义标签的实例代码的使用技巧和注意事项,需要的朋友参考一下 此篇文章内容仅限于 描述springboot与 thy 自定义标签的说明,所以你在看之前,请先会使用springboot和thymeleaf!! 之前写过一篇是springMVC与thymeleaf 的自定义标签

  • 问题内容: 在我的html中,我很好奇使用唯一标识符(例如 保存图片)而不是语义是否正确。例如: 是否优先拥有: 与CSS: 而不是:我目前有: 与CSS: 唯一标识符的使用在语义上是否正确? 问题答案: 最好避免使用自定义标签,因为您永远不知道这些标签何时会标准化,并在将来有特殊用途。 如果要避免使用标头标记,对于您的示例最好的操作如下: 此外: 如果在设计页面时不使用标准html标记,则在禁用

  • 本文向大家介绍谈一谈,使用标签库有什么好处?如何自定义JSP标签?相关面试题,主要包含被问及谈一谈,使用标签库有什么好处?如何自定义JSP标签?时的应答技巧和注意事项,需要的朋友参考一下 考察点:JSP标签   使用标签库的好处包括以下几个方面: - 分离JSP页面的内容和逻辑,简化了Web开发; - 开发者可以创建自定义标签来封装业务逻辑和显示逻辑; - 标签具有很好的可移植性、可维护性和可重用

  • 我正在尝试将管道分隔字符串转换为xml,这很好。但是,我没有得到我在下面展示的实际输出。在我的输出中,父标记与包名一起生成,它也没有生成我想要的名称空间。你能帮我把平面文件转换成我期望的精确输出吗。 CamelConfig。JAVA ConverterRoute。JAVA 来源txt Test.java 输出 测验xml 预期产量 测验xml

  • 问题内容: 如何创建自定义javadoc标记,例如@pre / @post?我找到了一些解释它的链接,但是我还没有运气。这些是一些链接: http://www.developer.com/java/other/article.php/3085991/Javadoc- Programming.html http://java.sun.com/j2se/1.5.0/docs/tooldocs/wind

  • 我在使用自定义标记库时发现了一个奇怪的问题。在我的一个jsp页面中,自定义tld文件的相对uri路径错误。 Taglib包含在jsp页面中,如下所示 此标记库包含在父jsp文件中,而不是从父jsp文件中导入/包含jsp文件。 我的应用程序还有几个其他jsp文件,其中相同的标记库与正确的路径一起使用。 相同的代码部署在多个测试环境中。在少数环境中,代码会抛出一个错误“找不到资源”,这是意料之中的。但