2017-09-21 36 views
0

我有以下代碼與Thymeleaf和Spring。對於一些列表,我有選定的選項的值填充整個對象,但對於一些它沒有。Thymeleaf選擇下拉框不填充完整的Java對象

public class BeneficiaryUploadCommand { 

    private List<SchemeCommand> schemeCommandList; 

    private List<BudgetHeads> budgetHeadsList; 

    @NotEmpty 
    private List<BeneficiaryType> beneficiaryTypeLists; 

    @NotEmpty 
    private List<FinancialYear> financialYearList; 

    @NotEmpty 
    private List<SubSchemes> subSchemesList; 

    private Date toDate; 

    private Date fromDate; 

    @Size(min=10, max = 10) 
    private String toDate1; 

    @Size(min=10, max = 10) 
    private String fromDate1; 

    @NotEmpty 
    private List<BenefitType> benefitTypesList; 

    @NotNull 
    private Integer beneficiariesProposed; 

    @NotNull 
    private Double stateShare; 

    private Double actualExpenditure; 

    @NotNull 
    private Double advancedExpenditure; 

    @NotNull 
    private char aadharLinkedOrNot; 

    @NotNull 
    private char cropItemDataAvailable; 

    @NotNull 
    private String schemeCommandId; 

    @NotNull 
    private String budgetHeadsListId; 
} 

Thymeleaf代碼如下:

 <select id="financialYearListId" th:field="*{financialYearList}" style="width:100px; float:left;" > 
      <option th:value="0" th:text=" Select "></option> 
      <th:block th:each="finYear : ${beneficiaryData.financialYearList}"> 
       <option th:value="${finYear.id}" th:text="${finYear.financialYear}" label=" - Select - "></option> 
      </th:block> 
     </select> 
    </td> 
    <td colspan="1" align="right"><font color="red">*</font> 
     <b>From Date</b> 
    </td> 
    <td colspan="1"> 
     <input type="text" th:field="*{fromDate1}" name="from_date" class="date form-control" style="width: 100px; margin: 0px;"/> 
    </td> 
    <td colspan="1" align="right"><font color="red">*</font> 
     <b>To Date</b> 
    </td> 
    <td colspan="1"> 
     <input type="text" th:field="*{toDate1}" name="to_date" class="date form-control" style="width: 100px; margin: 0px;"/> 
    </td> 
</tr> 
<tr> 
    <td colspan="1"><font color="red">*</font> <b>Scheme</b></td> 
    <td colspan="1" width="20%"> 
     <select th:field="*{schemeCommandId}" name="scheme_id" style="width:250px" th:onchange="'getSubSchemesandBudgetHeads(this.value);'"> 
      <th:block th:each="scheme : ${beneficiaryData.schemeCommandList}"> 
       <option th:value="${scheme.id}" th:text="${scheme.schemeName}" label=" - Select - " /> 
      </th:block> 
     </select> 

現在,當我選擇th:field="*{financialYearList}" 填充了List對象的整個financialyear對象。然而,schemeCommandList它只給出了ID?我很困惑一些對象怎樣才能被整體轉換,而有些對象只能提供id。

回答

0

的差別似乎是,對於 '財政年度' 下拉列表中,您的Java類有:

private List<FinancialYear> financialYearList; 

對應於以下Thymeleaf標記:

<select id="financialYearListId" th:field="*{financialYearList}"... 

然而,「方案命令「下拉列表中,具有以下標記:

<select th:field="*{schemeCommandId}"... 

這是填充Ja VA領域:

private String schemeCommandId; 

換句話說,在Thymeleaf標記告訴第一個下拉填充financialYearList場,其中第二個是唯一的告訴它來填充schemeCommandId領域,這似乎是你的效果看到了。

所以,我想象中的解決方案,以獲得您想要將改變Thymeleaf標記爲第二個下拉是什麼:

<select th:field="*{schemeCommandList}"... 
+0

感謝您的答覆。我用* {schemeCommandList}字段嘗試了它,我得到一個轉換異常。爲什麼它對一些而不是對某些人有用?它非常不穩定 – manojpotla

+0

它絕對不穩定!您遇到的問題將在您瞭解如何使用它!您需要比較您的'FinancialYear'類和'SchemeCommand'類之間的差異,以確定爲什麼有效,哪些沒有。 '轉換異常'會告訴你它無法將你選擇的值轉換成'SchemeCommand'。這可能會給你一個關於它失敗原因的具體原因 - 編輯你的帖子以包含它,如果有必要,還可以指定兩個類,並且有人可能會指出你的錯誤。 – DaveyDaveDave