2
我有一个使用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>
这是非常接近,但它实际上并没有设置用户对象上的选择下拉菜单中的值,因此其设置为每次空...
你可以把方法,其中返回编辑表单? –
请用表单的GET方法发布代码(将'user'加载到表单中)。也许你忘了将'INSTITUTION_LIST'添加到'model'中? – sanluck
我使用GET方法更新了问题,以及基于另一个StackOverflow发布来读取列表和匹配的最新方法。 – sonoerin