我完全改變這個問題,因爲它的一部分在Avnish的大力幫助下回答了here! 湯姆把我送到了正確的方向,所以謝謝湯姆!百里香多次選編輯
我的問題是,我不知道如何告訴Thymeleaf在編輯時預先選擇對象元素。
讓我告訴你:
此解決方案:
<select class="form-control" id="parts" name="parts" multiple="multiple" >
<option th:each="part : ${partsAtribute}"
th:selected="${servisAttribute.parts.contains(part)}"
th:value="${part.id}"
th:text="${part.name}">Part name</option>
</select>
我已經試過這樣:
<select class="form-control" th:field="*{parts}" multiple="multiple" >
<option th:each="part : ${partsAtribute}"
th:field="*{parts}"
th:value="${part.id}"
th:text="${part.name}">Part name</option>
</select>
沒有工作。我也試過這個:
<select class="form-control" th:field="*{{parts}}" multiple="multiple" >
<option th:each="part : ${partsAtribute}"
th:field="*{parts}"
th:value="${part.id}"
th:text="${part.name}">Part name</option>
</select>
也沒有工作。我已經嘗試從選項標記中刪除th:field =「* {parts}」,結果相同。
如果我將th:value更改爲$ {part},但它不會返回字符串像[2,4,5,6,...],但部分實例像[部分@ 43b45j,部分@ we43y7,...] ...
更新:我只是注意到,這隻適用於一部分選擇:
<select class="form-control" th:field="*{parts}" multiple="multiple" >
<option th:each="part : ${partsAtribute}"
th:field="*{parts}"
th:value="${part.id}"
th:text="${part.name}">Part name</option>
</select>
如果選擇多個部件,這是行不通的......
這是必須的:「在Part類中實現正確的equals()和hashcode()方法以確保正確比較也很重要。」 – Blejzer