首页 > 解决方案 > Spring MVC form:select selection from database issue

问题描述

I have populated a dropdown in JSP from controller by a list using following: ''' <form:select path="thirdPartyOccupationId" id="thirdPartyOccupationId" class="form-control input-sm"> <c:forEach var="thirdPartyProfession" items="${professionsList}"> <form:option value="${thirdPartyProfession.professionId}" label="${thirdPartyProfession.profession}" /> </c:forEach> </form:select> ''' thirdPartyOccupationId is from entity class. I have saved the selected value from this dropdown in database. Now when I reload the page and I don't see the value of thirdPartyOccupationId as selected. The dropdown just shows the list of the values in ascending order.

For example, I saved 5 as the value of thirdPartyOccupationId in db using the dropdown. When I reload the page the value 5 is not the selected value.

Same piece of code is working with a different field, I don't know what I am missing.

Model Classes:

ReportClass.java

    @JoinColumn(name = "third_party_occupation_id", referencedColumnName = "profession_id")
    @ManyToOne
    private Professions thirdPartyOccupationId;

Professions.java

        @Id
        @Basic(optional = false)
        @Column(name = "profession_id")
        private String professionId;
        @Column(name = "profession")
        private String profession;
        @OneToMany(mappedBy = "thirdPartyOccupationId")
        private Collection<ReportClass> reportClassCollection;

Controller.java

reportClass.setThirdPartyOccupationId(this.serviceManager.getProfessionsService().getByKey("123"));

model.addAttribute("reportClass", reportClass);
List<Professions> professionsList = serviceManager.getProfessionsService().findAll();
model.addAttribute("professionsList", professionsList);

Appreciate any pointers.

标签: springjspmodel-view-controller

解决方案


作为一种解决方法,我在 <c:forEach> 循环上方创建了一个新行,用于查看所选属性,即

<tr>
    <td>
        <c:if test="${tipFe !=null}">${tipFe}</c:if>
        <c:if test="${tipFe eq null}">-</c:if>
    </td>
</tr>

<tr>
    <td>
        <c:forEach var="ltipfe" items="${ltipfe}">
        <option value=${ltipfe.tipfe}>${ltipfe.tipfe}</option>
        </c:forEach>
    </td>
</tr>

推荐阅读