Thymeleaf选择下拉列表不填充完整的Java对象

时间:2017-09-21 05:54:53

标签: spring-mvc thymeleaf

我有以下代码与Thymeleaf和Spring。对于某些列表,我所选择的选项的值会填充整个对象,但对于某些列表却没有。

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}"时 填充financialyear对象的整个List对象。但是使用schemeCommandList它只会提供ID?我很困惑,如何将一些对象转换成整体,而有些只提供id。

1 个答案:

答案 0 :(得分:0)

差异似乎是,对于“财政年度”下拉列表,您的Java类具有:

private List<FinancialYear> financialYearList;

对应于以下Thymeleaf标记:

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

但是,'scheme命令'下拉列表有以下标记:

<select th:field="*{schemeCommandId}"...
填充Java字段的

private String schemeCommandId;

换句话说,Thymeleaf标记告诉第一个下拉列表填充financialYearList字段,第二个字段只是告诉它填充schemeCommandId字段,这似乎是效果你看到了。

所以,我认为获得你想要的解决方案是将第二次下拉列表中的Thymeleaf标记改为:

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