【问题标题】:Thyme leaf-SpringBoot-Targetting Map.Entry within Map property of Form-bound objectThymeleaf-Spring Boot-Targeting Map.Entry 在表单绑定对象的 Map 属性中
【发布时间】:2016-07-13 08:33:36
【问题描述】:

找不到任何例子或方法,终于注册到这个论坛寻求帮助! 我的应用程序是问卷 - 我显示问题,用户从下拉列表中选择答案。 我有两个模型属性 1)问题对象列表“questionList”是模型中的变量名 2) salesResponse 是另一个属性的变量名 - 它是 SalesResponse 类的对象。 SalesResponse 有一个名为“responses”的属性,它是一个 TreeMap。它有一个默认的构造函数,以及用于“响应”的 setter/getter。 在 POST 上,key 应该是问题的 ID,value 应该是所选选项的值。 所以,应该是TreeMap。这是当用户执行 POST 时我希望在我的 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>

这应该怎么写? Spring 是否足够聪明,可以一次填充响应 TreeMap,One Map.Entry?

编辑:我试过这个,但它没有用(得到这个例外),但也许它给了某人解决这个问题的灵感! java.lang.NumberFormatException: For input string: "iterStat.index"

试用码:

<form action="#" th:action="@{/ehc}" th:object="${salesResponse}"
method="post">
<table>
<tr th:each="question,iterStat: ${questionList}">
    <td><input type="number" readonly="readonly"
        th:text="${question.questionText}" th:name="${responses}" 
        th:value="${question.ID}" 
        th:field="*{responses[iterStat.index].key}"/></td>
    <td><select th:field="*{responses[iterStat.index].value}">
      <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>

【问题讨论】:

    标签: java spring-mvc spring-boot thymeleaf


    【解决方案1】:

    经过很多不眠之夜,我已经开始工作,并将其记录下来,以便其他尝试此操作的人受益: 这是视图中的代码:

    <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 时,您可以在选择的名称字段中看到“responses[n]”。这本质上是指结果映射的“键”,而值字段是相应的值。 现在,完成此操作后,我确实注意到当我使用“th:field”时,我的响应 getter 被调用了 300 次,仅用于 Map 中的 15 个条目。我用 th:name 替换了 th:field 以生成相同的 html,现在它只被调用了 15 次。尝试在网上搜索原因,但没有找到任何东西。现在通过,我很高兴继续前进,因为在这个障碍上花了这么多时间!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 2018-07-24
      • 2022-01-22
      • 2015-02-01
      • 2017-09-12
      • 1970-01-01
      相关资源
      最近更新 更多