0
我有這樣的形式:Thymeleaf - 不同量的參數
<form th:action="@{'/articles/' + ${article.id} + '/processTest'}" method="post">
<table>
<tr th:each="entry,iter: ${wordsWithTranslation}">
<td><input type="text" th:value="${entry.key.value}" th:name="'q' + ${iter.index}" readonly="readonly"/>
</td>
<td> -----</td>
<td><input type="text" th:name="'a' + ${iter.index}"/></td>
</tr>
</table>
<br/>
<input type="submit" value="Sprawdź"/>
</form>
wordsWithTranslation是其可以包含不同量元素的HashMap中。
而且控制器:
public String processTest(Model model, @PathVariable Long id,
@ModelAttribute(value = "q0") String q0,
@ModelAttribute(value = "a0") String a0,
@ModelAttribute(value = "q1") String q1,
@ModelAttribute(value = "a1") String a1)
我怎麼能解決這個問題的方法參數不(對每個Q和值的ModelAttribute)做這樣的事情?有沒有什麼辦法可以像循環這樣做,或者什麼是最好的解決方案?
它適用於RequestParam,但不適用於ModelAttribute。 qList中只有1個元素,而aList中只有0個元素。 – Helosze
@Helosze @ @ ModelAttribute'對綁定參數沒有任何作用。這個註解只是表示方法的參數應該添加到具有指定名稱的模型中。你可以根本刪除'@ ModelAttribute',但是值將被綁定。 –