我有一个使用Thymeleaf的Spring Boot 1.3应用程序。我有一个屏幕,显示用户列表,包括他们与之关联的机构。感谢之前的一些StackOverflow帮助,我可以创建一个新用户并从下拉列表中选择他们的机构。我现在正在尝试为编辑用户做类似的事情 - 当您编辑记录时,下拉默认为用户已被分配的机构。
我有一个用户:
@Entity
@Table(name = "Users")
public class User implements UserDetails {
@Id
private String username;
...
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "institutionId", nullable = false)
private Institution institution;
}
和一个机构:
@Entity
@Table(name = "Institution")
public class Institution {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long institutionId;
private String name;
@OneToMany(mappedBy = "institution", fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
List<User> users = new ArrayList<User>();
}
这是我的HTML编辑用户记录(已编辑):
我附加了屏幕截图,显示有指定机构的用户,但是当我尝试编辑时,没有下拉列表。
如何获得该下拉菜单,并且最好使用user.institution预先选择?
编辑以显示进度并清理问题: 我有它,以便当我去编辑时,列表会显示给用户的机构。
<div class="col-md-5">
<div th:if="${user.institution != null }"
<select name="user.institution">
<option th:each="choice : ${institutionList}"
th:value="${choice.institutionId}"
th:attr="choiceinstitutionId=${choice.institutionId}, institutioninstitutionId=*{institution.institutionId}, showselected=(${choice.institutionId} == *{institution.institutionId})"
th:selected="(${choice.institutionId} == *{institution.institutionId})"
th:readonly="(${choice.institutionId} == *{institution.institutionId})"
th:text="${choice.name}">
</option>
</select>
</div>
<div th:if="${user.institution == null }">
<div th:if="${institutionList != null and not #lists.isEmpty(institutionList)}">
<select name="user.institution">
<option th:each="dropDownItem : ${institutionList}"
th:value="${dropDownItem.institutionId}"
th:text="${dropDownItem.name}" />
</select>
</div>
<div th:if="${institutionList == null or #lists.isEmpty(institutionList)}">
<div>"No Institutions were found, please create some first"</div>
</div>
</div>
<div th:if="${institutionList == null or #lists.isEmpty(institutionList)}">
<div>"No Institutions were found, please create some first"</div>
</div>
</div>
这非常接近,除了它实际上没有在用户对象上设置选定的下拉值,因此每次都设置为null ...
答案 0 :(得分:3)
原来这是一个愚蠢的错误 - 实际上没有声明要设置的字段。如果其他人遇到类似的问题,这里有正确的代码来显示列表中的任何选定值,并设置它:
<div th:if="${user.institution != null }">
<select name="user.institution" th:field="*{institution}">
<option th:each="choice : ${institutionList}"
th:value="${choice.institutionId}"
th:attr="choiceinstitutionId=${choice.institutionId}, institutioninstitutionId=*{institution.institutionId}, showselected=(${choice.institutionId} == *{institution.institutionId})"
th:selected="(${choice.institutionId} == *{institution.institutionId})"
th:readonly="(${choice.institutionId} == *{institution.institutionId})"
th:text="${choice.name}"></option>
</select>
</div>
<div th:if="${user.institution == null }">
<div th:if="${institutionList != null and not #lists.isEmpty(institutionList)}">
<select name="user.institution" th:field="*{institution}">
<option th:each="dropDownItem : ${institutionList}"
th:value="${dropDownItem.institutionId}"
th:text="${dropDownItem.name}" />
</select>
</div>
<div th:if="${institutionList == null or #lists.isEmpty(institutionList)}">
<div>"No Institutions were found, please create some first"</div>
</div>
</div>