spring - 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.
解决方案
作为一种解决方法,我在 <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>
推荐阅读
- regex - 如何使用正则表达式扫描在 .c 文件中定义具有初始大写卡梅尔案例名称的静态变量的代码行?
- angular - 如何创建一条从 x,y 坐标开始到其他 x,y 坐标的线?
- php - 使用 laravel 插入当前日期时间
- javascript - 在反应生命周期中等待 Redux thunk
- php - 使用 PHP 在重复时将 ID 加一
- sql - 过时数据计数
- postgres-9.6 - Postgres 备份恢复问题
- typescript - 实现由类实现的接口时出现令人困惑的错误
- elasticsearch - 全数组的 Elasticsearch 聚合
- hibernate - 为什么在 jboss 7.1.1 社区版的两台服务器中部署时休眠 ID 没有增加