如何在thymeleaf中以递归的形式添加参数化片段?

eulz3vhy  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(292)

我有一个数据结构,它包含原始值和复杂值。

public class Topic{
    private String topicUrl = "";
    private String key = "";
    private String name = "";
    private String value = "";
    private String type;//RECORD/ARRAY/ENUM/PRIMITIVE
    private List<String> enumValues = new ArrayList<>();
    private List<Topic> fieldList = new ArrayList<>();
    //getters setters
    //isEnum()
    //isPrimitive()
    //isRecord()
}

我已经创建了thymeleaf前端来提交主题对象。前端只能编辑基本体对象的值字段。示例对象与submit as json之前类似:

{
  "topicUrl": "url",
  "key": "",
  "name": "object1",
  "value": "",
  "type": "RECORD",
  "fieldList": [
    {
      "topicUrl": "",
      "key": "",
      "name": "object1.1",
      "value": "",
      "type": "INT",
      "fieldList": []
    },
    {
      "topicUrl": "",
      "key": "",
      "name": "object1.2",
      "value": "",
      "type": "RECORD",
      "fieldList": [
        {
          "topicUrl": "",
          "key": "",
          "name": "object1.2.1",
          "value": "",
          "type": "INT",
          "fieldList": []
        }]
    }]
}

作为json提交后:

{
    "topicUrl" : "url",
    "key" : "",
    "name" : "object1",
    "value" : "",
    "type" :"RECORD",
    "fieldList":[{
        "topicUrl" : "",
        "key" : "",
        "name" : "object1.1",
        "value" : "11",
        "type" :"INT",
        "fieldList":[]
    },{
        "topicUrl" : "",
        "key" : "",
        "name" : "object1.2",
        "value" : "",
        "type" :"RECORD",
        "fieldList":[{
            "topicUrl" : "",
            "key" : "",
            "name" : "object1.2.1",
            "value" : "121",
            "type" :"INT",
            "fieldList":[]
        }]
}]}

表单页

<form th:action="@{/writeTopic}" th:object="${selectedTopic}" method="post" class="form-horizontal">
    <fieldset>
        <input type="submit" class="btn btn-primary" th:value="Write"></input>
        <table class="table table-striped table-bordered">
        <tbody>
                <tr>
                    <td th:text="TopicUrl"></td>
                    <td>
                       <input readonly th:field="*{topicUrl}" class="form-control form-control-lg"/>
                    </td>
                </tr>
                <tr>
                    <td th:text="Key"></td>
                    <td>
                       <input th:field="*{key}" class="form-control form-control-lg"/>
                    </td>
                </tr>
        </table>
        <div th:replace="fragments/topicFieldsTable  :: topicFieldsTable(topic=${selectedTopic})"></div>
    </fieldset>
</form>

如果类型为“record”,则每个topic对象都包含fieldlist中的topic元素。所以我创建了topicfieldstable.html片段用于递归

<table th:fragment="topicFieldsTable(topic)"  class="table table-bordered table-striped">
    <thead class="thead-dark">
        <tr>
            <th scope="col" th:text="${topic.className}"></th>
            <th scope="col">Type</th>
            <th scope="col">Value</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="field, itemStat : ${topic.fieldList}">
            <td>
                <!--field.name-->
                <label th:text="${field.name}">Name</label>
            </td>
            <td>
                <!--field.type-->
                <label th:text="${field.type}">Type</label>
            </td> 
            <td th:if="${field.isEnum()}">
                <select class="form-control form-control-lg">
                    <option th:each="eval : ${field.enumValues}" 
                    th:value="${eval}" 
                    th:text="${eval}">Value</option>
                </select>
            </td>
            <td th:if="${field.isRecord()}">
                <div th:replace="fragments/topicFieldsTable  :: topicFieldsTable(topic=${field})"></div>
            </td>
            <td th:if="${field.isPrimitive()}">
                <!--field.value-->
               <input name="value" th:name="${field.name}" th:value="${field.value}" class="form-control form-control-lg"/>
            </td>
        </tr>
    </tbody>
</table>

另外,我不能在片段中用th:field分配字段。你知道吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题