Spring mvc 如何使用Thymeleaf标准表达式语法比较模型的两个变量?
我正在尝试使用ThymileAF+springMVC设置选中的Spring mvc 如何使用Thymeleaf标准表达式语法比较模型的两个变量?,spring-mvc,thymeleaf,Spring Mvc,Thymeleaf,我正在尝试使用ThymileAF+springMVC设置选中的选项的属性,如下所示: (item.codItem和defaultCodItem是长的) 564 || ? 但结果是 <select id="selItems"> <option selected="selected" value="455">11/2014 - 455 || 450</option> <option selected="selected" value="4
选项的属性,如下所示:
(item.codItem
和defaultCodItem
是长的)
564 || ?
但结果是
<select id="selItems">
<option selected="selected" value="455">11/2014 - 455 || 450</option>
<option selected="selected" value="450">450 || 450</option>
<option selected="selected" value="452">452 || 450</option>
<option selected="selected" value="457">457 || 450</option>
<option selected="selected" value="453">453 || 450</option>
<option selected="selected" value="454">454 || 450</option>
<option selected="selected" value="451">451 || 450</option>
</select>
11/2014 - 455 || 450
450 || 450
452 || 450
457 || 450
453 || 450
454 || 450
451 || 450
当我预料到这一点时:
<select id="selItems">
<option selected="" value="455">11/2014 - 455 || 450</option>
<option selected="selected" value="450">450 || 450</option>
<option selected="" value="452">452 || 450</option>
<option selected="" value="457">457 || 450</option>
<option selected="" value="453">453 || 450</option>
<option selected="" value="454">454 || 450</option>
<option selected="" value="451">451 || 450</option>
</select>
11/2014 - 455 || 450
450 || 450
452 || 450
457 || 450
453 || 450
454 || 450
451 || 450
那么,我应该如何将item.codItem
与模板中的defaultCodItem
进行比较,以正确设置所选的
属性?根据Thymeleaf文档:
标准方言包括允许您通过计算条件来设置这些属性的属性,因此如果计算为
如果为true,则该属性将设置为其固定值,如果计算为false,则不会设置该属性
“选定”是其中之一。我认为您应该简单地将条件写为:
th:selected="${item.codItem} eq ${defaultCodItem}"
我猜您的当前行无法按预期工作,因为非空值的计算结果为true(例如您的空字符串)请在向下投票时添加对正
th:selected="${item.codItem} eq ${defaultCodItem}"