我完全改变这个问题,因为它的一部分在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