Spring Boot - 如何在下拉列表中检索所选项目?

时间:2016-01-26 17:42:32

标签: database spring drop-down-menu spring-boot

我在用户编辑屏幕中遇到以下问题:

想象一下以下课程用户和国家, 我每个人都有一张桌子,这种关系是一对多的。

在编辑屏幕的会话中,我们在用户注册的国家/地区显示了一个 下拉以更新此信息, 但是我想把当前用户的国家和其他国家选中,我该怎么办?

<div class="form-group">
	<label class="col-sm-2 control-label" for="country">Country</label>

	<div class="col-sm-6 col-xs-12">
		<select id="country" th:field="*{user.name}" class="form-control"
				required="required">
			<option value="">Select...</option>
			<option th:each="c : ${countries}" th:value="${c.id}"
					th:text="${c.name}" value=""></option>
		</select>
	</div>
</div>

我正在使用Thymeleaf和Springboot。

解决

要使代码生效,只需改变这种方式:

<div class="form-group">
	<label class="col-sm-2 control-label" for="country">Country</label>

	<div class="col-sm-6 col-xs-12">
		<select id="country" name="user.country" class="form-control"
				required="required">
			<option value="">Select...</option>
			<option th:each="c : ${countries}" th:value="${c.id}"
					th:text="${c.name}" th:selected="(${c.id} == *{user.country.id})"></option>
		</select>
	</div>
</div>

我在link中找到了解决方案:

“根据thymeleaf论坛(post)th:字段不适用于:选择”

0 个答案:

没有答案