【发布时间】: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