如何在 Thymeleaf 中将 th:selected 与 HashMap entrySet() 一起使用?

How to use th:selected with HashMap entrySet() in Thymeleaf?

我试过将 selected 放在空选项中,也尝试将 th:selected 放在 th:text 之后,两者都不适合我,这是我的代码:

<select name="uploadDate"style=" width:100%;" id="uploadDate" class="form-control select2">
        <option></option>
        <option th:each="uploadDateMenu : ${uploadDateMenu.entrySet()}"
                th:value="${uploadDateMenu.key}" 
                th:text="${uploadDateMenu.value}">
      </option>
</select>

我试过输入代码:

th:selected="${uploadDateMenu.key == 2}"

在选项标签内的 "th:text" 之后,但它不起作用。数据在键值对中。其中key为2,对应的value为"Last 2 days".

尝试:

th:selected="${uploadDateMenu.getKey() == 2}"

或者如果不行,重写如下:

   <option th:each="uploadDateMenuEntry : ${uploadDateMenu.entrySet()}"
                th:value="${uploadDateMenuEntry.key}" 
                th:text="${uploadDateMenuEntry.value}"
                th:selected="${uploadDateMenu.get(2)}">

      </option>