Java 百里香叶SpringBoot目标映射。表单绑定对象的映射属性中的条目

Java 百里香叶SpringBoot目标映射。表单绑定对象的映射属性中的条目,java,spring-mvc,spring-boot,thymeleaf,Java,Spring Mvc,Spring Boot,Thymeleaf,找不到任何例子或方法来做到这一点,最后注册到这个论坛寻求帮助! 我的应用程序是一个调查问卷-我显示问题,用户从下拉列表中选择答案。 我有两个模型属性 1) 问题对象列表“questionList”是模型中的变量名 2) salesResponse是另一个属性的变量名,它是salesResponse类的对象。 SalesResponse有一个名为“responses”的属性,它是一个树映射。它有一个默认构造函数,以及“responses”的setter/getter。 在POST上,键应该是问题的

找不到任何例子或方法来做到这一点,最后注册到这个论坛寻求帮助! 我的应用程序是一个调查问卷-我显示问题,用户从下拉列表中选择答案。 我有两个模型属性 1) 问题对象列表“questionList”是模型中的变量名 2) salesResponse是另一个属性的变量名,它是salesResponse类的对象。 SalesResponse有一个名为“responses”的属性,它是一个树映射。它有一个默认构造函数,以及“responses”的setter/getter。 在POST上,键应该是问题的ID,值应该是所选选项的值。 所以,应该是树形图。这是我希望在用户发布帖子时在salesResponse对象中填充的映射

我的控制器将这两个对象加载到上下文中…我不知道如何编写表单!以下是我大致需要的:

<form action="#" th:action="@{/ehc}" th:object="${salesResponse}"
method="post">
<table>
  <tr th:each="question: ${questionList}">
    <td th:text="${question.questionText}" th:value="${question.ID}"  
        th:name=${..what??..}
        th:field="*{THIS SHOULD BECOME the KEY in responses TreeMap}"></td>
    <td><select th:field="*{THIS SHOULD BECOME THE VALUE in responses TreeMap}">
      <option th:text="${question.option1}" th:value="2"></option>
      <option th:text="${question.option2}" th:value="4"></option>
      <option th:text="${question.option3}" th:value="6"></option>         
    </select> 
  </td>
</tr>
<tr>
    <td><input type="submit" value="Submit" /></td>
    <td><input type="reset" value="reset" /></td>
</tr>
</table>
</form>

在经历了许多不眠之夜之后,我已经让它工作了,并且记录了它,以便其他人可以从中受益: 以下是视图中的代码:

<form action="#" th:action="@{/ehc}" th:object="${salesResponse}" 
method="post">

<table>
<tr th:each="entry,iterStat: ${responses}">
  <td th:text="${salesResponse.questionList[iterStat.index].questionText}"></td>
  <td><select th:name="'responses[' + ${entry.key} +']'">
<!--  <td><select th:field="*{responses[__${entry.key}__]}">-->
      <option selected="selected" th:text="${salesResponse.questionList[iterStat.index].option1}"
                                  th:value="2" ></option>
      <option th:text="${salesResponse.questionList[iterStat.index].option2}"
              th:value="4" ></option>
      <option th:text="${salesResponse.questionList[iterStat.index].option3}"
              th:value="6" ></option>         
    </select> 
  </td>

</tr>
<tr>
    <td><input type="submit" value="Submit" /></td>
    <td><input type="reset" value="reset" /></td>
</tr>
</table>

</form>

关键因素是正确选择标记。呈现HTML时,您可以在select的name字段中看到“responses[n]”。基本上是指结果映射的“键”,值字段是相应的值。 现在,在完成这项工作之后,我确实注意到,当我使用“th:field”时,我的响应getter被调用了300次,只调用了映射中的15个条目。我用th:name替换了th:field以生成相同的html,现在它只被调用了15次。尝试在网上搜索原因,但未找到任何内容。现在,我很高兴继续前进,因为我在这个路障上花了这么多时间

<form action="#" th:action="@{/ehc}" th:object="${salesResponse}" 
method="post">

<table>
<tr th:each="entry,iterStat: ${responses}">
  <td th:text="${salesResponse.questionList[iterStat.index].questionText}"></td>
  <td><select th:name="'responses[' + ${entry.key} +']'">
<!--  <td><select th:field="*{responses[__${entry.key}__]}">-->
      <option selected="selected" th:text="${salesResponse.questionList[iterStat.index].option1}"
                                  th:value="2" ></option>
      <option th:text="${salesResponse.questionList[iterStat.index].option2}"
              th:value="4" ></option>
      <option th:text="${salesResponse.questionList[iterStat.index].option3}"
              th:value="6" ></option>         
    </select> 
  </td>

</tr>
<tr>
    <td><input type="submit" value="Submit" /></td>
    <td><input type="reset" value="reset" /></td>
</tr>
</table>

</form>