【问题标题】:Spring Boot - Thymeleaf and Json TemplatesSpring Boot - Thymeleaf 和 Json 模板
【发布时间】:2019-10-31 03:04:36
【问题描述】:

我正在使用 Spring Boot 2.1.5.RELEASE 和 wnat 使用 Thymeleaf 生成 json 模板文件。

这是我目前拥有的 1 个文件的示例

 {
  "apiVersion": "apps/v1",
  "kind": "Deployment",
  "metadata": {
    "labels": {
      "run": "[( ${imageNameLabel} )]"
    },
    "name": "[( ${imageNameLabel} )]"
  },
  "spec": {
    "replicas": [( ${replicas} )],
    "selector": {
      "matchLabels": {
        "run": "[( ${imageNameLabel} )]"
      }
    },
    "template": {
      "metadata": {
        "labels": {
          "run": "[( ${imageNameLabel} )]"
        }
      },
      "spec": {
        "containers": [
          {
            "image": "[( ${imageName} )]:[( ${imageVersion} )]",
            "name": "[( ${imageNameLabel} )]",
            "env": [( ${credentials} )],
            "volumeMounts": [
                            {
                                "mountPath": "/etc/foo",
                                "name": "[( ${imageNameLabel} )]",
                                "readOnly": true
                            }
                        ]
          }
        ],
        "volumes": [
                    {
                        "name": "[( ${imageNameLabel} )]",
                        "secret": {
                            "secretName": "[( ${imageNameLabel} )]"
                        }
                    }
                ]
      }
    }
  }
}

我现在想把条件逻辑放到这个文件中。 thymeleaf 可以做到这一点,还是我应该研究另一种模板技术?

谢谢 达米安

【问题讨论】:

    标签: java spring spring-boot templates thymeleaf


    【解决方案1】:

    请参阅textual template mode 的文档。一个示例 if 可能如下所示:

    [# th:if="${condition}" th:text="${item}" /]
    

    或者

    [# th:if="${condition}"][(${imageNameLabel})][/]
    

    【讨论】:

      猜你喜欢
      • 2016-01-26
      • 2017-03-08
      • 2017-06-21
      • 1970-01-01
      • 2021-09-25
      • 2020-03-14
      • 2020-10-07
      • 2016-01-27
      • 2019-04-11
      相关资源
      最近更新 更多