【问题标题】:Thymeleaf select dropdown not populating full Java objectThymeleaf 选择下拉列表未填充完整的 Java 对象
【发布时间】:2017-09-21 17:40:36
【问题描述】:

我在 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}" 填充List 对象的整个financialyear 对象。但是对于schemeCommandList,它只给出id?我很困惑如何将某些对象整体转换,而有些对象只提供 id。

【问题讨论】:

    标签: spring-mvc thymeleaf


    【解决方案1】:

    不同之处似乎在于,对于“财政年度”下拉菜单,您的 Java 类具有:

    private List<FinancialYear> financialYearList;
    

    对应于以下 Thymeleaf 标记:

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

    但是,“方案命令”下拉菜单具有以下标记:

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

    正在填充 Java 字段:

    private String schemeCommandId;
    

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

    所以,我想获得您想要的解决方案是将第二个下拉列表的 Thymeleaf 标记更改为:

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

    【讨论】:

    • 感谢您的回复。我使用 *{schemeCommandList} 字段进行了尝试,但出现了转换异常。为什么它对某些人有效,而对某些人无效?它非常不稳定
    • 绝对不是不稳定的!您遇到的问题将在于您对如何使用它的理解!您需要比较您的 FinancialYear 类和您的 SchemeCommand 类之间的差异,以确定一个有效而一个无效的原因。 “转换异常”将告诉您它无法将您选择的值转换为SchemeCommand。它可能会为您提供失败的具体原因 - 编辑您的帖子以包含该内容,并在必要时包含两个类,并且有人可能会指出您的错误。
    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多