【发布时间】:2017-05-31 15:01:44
【问题描述】:
我有一个可以分配给 WorkOrder 的用户下拉列表。其中 User 到 WorkOrder 是 OneToMany 关系。问题是如何将 NULL 值分配给 user Id ?如果我不将它设置为 NULL,它可以正常工作,我可以从列表中分配任何名称。
如果我使用此代码并在我的 jsp 中选择 NULL <form:option value="${null}" label="null" />,我会收到错误消息:
Failed to convert property value of type java.lang.String to required type int for property user.id; nested exception is java.lang.NumberFormatException: For input string: ""
这是我的 WorkorderDAOImpl
@Override
public void saveWorkOrder(WorkOrder theWorkOrder) {
// get the current hibernate session
Session currentSession = sessionFactory.getCurrentSession();
User user = theWorkOrder.getUser();
System.out.println("workorder: " + theWorkOrder);
System.out.println("user: " + user);
//save/update the work order
currentSession.saveOrUpdate(theWorkOrder);
}
我的 workorder-form.jsp 我省略了一些标签
<form:errors path="workorder.*"/>
<form:errors path="user.*"/>
<form:form action="saveWorkOrder" modelAttribute="workorder" method="POST">
<!-- need to assotiate the data with workorder id -->
<form:hidden path="id"/>
<table>
<tbody>
<tr>
<td><label>User name:</label></td>
<td><form:input path="user.userName"/></td>
</tr>
<tr>
<td><label>User:</label></td>
<td><form:select path="user.id">
<form:option value="${null}" label="null" />
<form:options items="${users}"
itemLabel="userName" itemValue="id"
/>
</form:select>
【问题讨论】:
标签: java html hibernate jsp spring-mvc