2016-03-02 52 views
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預選?

enter image description here 在這裏,您可以看到編輯用戶不具有下拉:

enter image description here

編輯顯示進度和清理的問題: 我有這麼出現的列表當我去編輯時向用戶顯示該機構。

<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> 

這是非常接近,但它實際上並沒有設置用戶對象上的選擇下拉菜單中的值,因此其設置爲每次空...

+0

你可以把方法,其中返回編輯表單? –

+0

請用表單的GET方法發佈代碼(將'user'加載到表單中)。也許你忘了將'INSTITUTION_LIST'添加到'model'中? – sanluck

+0

我使用GET方法更新了問題,以及基於另一個StackOverflow發佈來讀取列表和匹配的最新方法。 – sonoerin

回答

2

原來這是一個愚蠢的錯誤 - 實際上並未聲明要設置的字段。萬一別人運行到類似的問題,這裏是正確的代碼在列表中顯示任何選擇的值,並設置:

<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> 
相關問題