【问题标题】:Using custom tags with thymeleaf使用带有百里香叶的自定义标签
【发布时间】:2015-01-27 00:58:11
【问题描述】:

如果我在我的 spring-boot 项目中添加一些自定义标签,是否可以在基于 thymeleaf 的文件中使用它们?例如,如果我在我的 TLD 文件中定义了这个标签(放在目录 /src/main/resources/static 中):

  <tag>
    <name>Input</name>
    <tag-class>org.store.custom.taglib.form_control.InputTag</tag-class>
  </tag>

我可以在我的视图中这样使用它吗:

<p th:case="'Input'"><f:Input/></p>

有人知道这是否可能吗?

更新

我尝试过,但是当我运行应用程序时,自定义标签没有被处理成相应的标签:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="/form.tld" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
  <title>cadastro</title>
</head>
<body>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title">cadastro</h3>
    </div>
    <div class="panel-body">
      <f:form th:attr="action=@{/__${command.getClass().getSimpleName()}__/cadastra}">
        <div th:each="item : ${command['class'].declaredFields}">
          <div th:each="item2 : ${item.declaredAnnotations}">
            <div th:switch="${item2.annotationType().simpleName}" th:with="index=${itemStat.index}">
              <p th:case="'Checkbox'"><f:Checkbox/></p>
              <p th:case="'DataList'"><f:DataList/></p>
              <p th:case="'Input'"><f:Input/></p>
              <p th:case="'Radiobutton'"><f:Radiobutton/></p>
              <p th:case="'Select'"><f:Select/></p>
              <p th:case="'Textarea'"><f:Textarea/></p>
            </div>
          </div>
        </div>
      </f:form>
    </div>
  </div>
</body>
</html>

【问题讨论】:

    标签: spring taglib thymeleaf


    【解决方案1】:

    没有。 Thymeleaf 的基本原则是不应该存在外来标签。 (仅标准 html)。所有动态内容都是通过自定义属性添加的。

    【讨论】:

    • 附加信息:百里香中唯一的(合成)元素/标签是th:block
    【解决方案2】:

    你可以这样使用

    <input th:attr="your string message ='\'' + #{yourObject.meesage}" + '\''"/>
    

    更多示例:https://attacomsian.com/blog/thymeleaf-custom-html-attributes

    【讨论】:

      猜你喜欢
      • 2015-06-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 2017-10-21
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多