Thymeleaf 下拉菜单中的默认值

Kin*_*ere 4 html spring spring-mvc thymeleaf

我正在使用 Spring MVC 和 thymeleaf 构建一个 Web 应用程序。我的下拉菜单是这样的,它按预期工作:

<form style="display: inline-block" th:action="@{/search}"
                th:object="${searchForm}" th:method="post">
                <select th:field="*{selectedOption}">
                    <option th:each="option : ${searchOptions}"
                        th:value="${option.getOption()}"
                        th:text="${option.getOptionName()}">Options</option>
                </select> <input type="text" th:field="*{criteria}" name="searchTextBox"
                    class="topcoat-text-input--large" /> <input type="submit"
                    style="display: inline-block" class="topcoat-button--large--cta"
                    value="Search" name="searchButton" />
            </form>
Run Code Online (Sandbox Code Playgroud)

但是如何为下拉菜单设置预选/默认值?

谢谢

编辑 1:

我试着添加这个:th:selected="${searchCriteria.getSelectedOption()}"让它成为:

<select th:field="*{selectedOption}">
                        <option th:each="option : ${searchOptions}"
                            th:value="${option.getOption()}"
                            th:text="${option.getOptionName()}"
                            th:selected="${searchCriteria.getSelectedOption()}">Options</option>
                    </select>
Run Code Online (Sandbox Code Playgroud)

但这仍然没有将默认值设置为所选内容。

Kar*_*ski 5

我想这searchCriteria.getSelectedOption()不会返回布尔值,但它必须返回。

编辑:

<select th:field="*{selectedOption}">
    <option th:each="option : ${searchOptions}"
            th:value="${option.getOption()}"
            th:text="${option.getOptionName()}"
            th:selected="${searchCriteria.isSelected(option)}">
        Options
    </option>
</select>
Run Code Online (Sandbox Code Playgroud)

在您的 SearchCriteria 类中(我不知道它实际上是什么样子):

public boolean isSelected(Option option) {
    return option.equals(selectedOption);
}
Run Code Online (Sandbox Code Playgroud)