Thymeleaf下拉菜单中的默认值 [英] Default value in drop down menu in Thymeleaf
问题描述
我正在使用Spring MVC和thymeleaf构建一个Web应用程序。我的下拉菜单是这样的,它可以像预期的那样工作:
< form style =display:inline-blockth :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()}>选项< /选项>
< / select> < input type =textth:field =* {criteria}name =searchTextBox
class =topcoat-text-input - large/> < input type =submit
style =display:inline-blockclass =topcoat-button - large - cta
value =Searchname =searchButton/> ;
< / form>
但是我怎样才能为下拉菜单设置预选/默认值?
谢谢
编辑1:
I尝试添加: 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()}>选项< / option>
< / select>
但是,这仍然不会将默认值设置为所选内容。
我想
编辑:
< ;选择th:field =* {selectedOption}>
< option th:each =option:$ {searchOptions}
th:value =$ {option.getOption()}
th:text =$ {option。 getOptionName()}
th:selected =$ {searchCriteria.isSelected(option)}>
选项
< / option>
< / select>
在您的SearchCriteria类中(我不知道实际外观如何):
public boolean isSelected(Option option){
return option.equals(selectedOption);
}
I am building a web application using Spring MVC and thymeleaf. My drop down menu is like this and it works as expected:
<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>
But how can I set a pre-selected/default value for the drop down menu?
Thanks
EDIT 1:
I tried adding this: th:selected="${searchCriteria.getSelectedOption()}"
to have it be:
<select th:field="*{selectedOption}">
<option th:each="option : ${searchOptions}"
th:value="${option.getOption()}"
th:text="${option.getOptionName()}"
th:selected="${searchCriteria.getSelectedOption()}">Options</option>
</select>
But this still does not set the default value to what is selected.
I suppose that searchCriteria.getSelectedOption()
doesn't return boolean value, but it has to.
Edit:
<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>
In your SearchCriteria class (I don't know how it actually look like):
public boolean isSelected(Option option) {
return option.equals(selectedOption);
}
这篇关于Thymeleaf下拉菜单中的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!