0
我有一個表cards
它與一些其他主表(分部和單位)與一對一的關係有關。執行一對一的關係船春天休眠
在UI我顯示下拉菜單選擇卡片的部門和單位的價值,這是CardController.java
@RequestMapping(value = "/addCardForm", method = RequestMethod.GET)
public String addCardForm(ModelMap map)
{
map.addAttribute("divisions", divisionService.getAllDivisions());
map.addAttribute("units", unitService.getAllUnits());
return "admin/addCard";
}
addCard.jsp
創建表單功能:
<form:form method="post" action="addCard">
<table cellspacing="10" id="card-table">
<tr>
<td><label for="division" class="control-label">Division : </label></td>
<td><select name="division" class="selectpicker">
<option>Select</option>
<c:forEach items="${divisions}" var="division">
<option value="${division.id}">${division.name}</option>
</c:forEach>
</select></td>
</tr>
<tr>
<td><label for="unit" class="control-label">Unit : </label></td>
<td><select name="unit" class="selectpicker">
<option>Select</option>
<c:forEach items="${units}" var="unit">
<option value="${unit.id}">${unit.name}</option>
</c:forEach>
</select></td>
</tr>
<tr>
<td><button type="submit" class="btn btn-primary">Submit</button></td>
<td></td>
</tr>
</table>
</form>
下拉菜單中填充與數據一起提交,但未將其設置的分區或單元提交給cardEntity對象時,它們被設置爲空,這是控制器中的addCard函數:
@RequestMapping(value = "/addCard", method = RequestMethod.POST)
public String addCard(@ModelAttribute(value="card") CardEntity card, BindingResult result)
{
cardService.addCard(card);
//card.getDivision(); -- this is null
return "redirect:/card";
}
除了下拉菜單外,卡中還有其他的字段。
CardEntity.java
@Entity
@Table(name="cards")
@Proxy(lazy=false)
public class CardEntity {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne
@JoinColumn(name="division_id", referencedColumnName="id")
private DivisionEntity division;
@OneToOne
@JoinColumn(name="unit_of_qty_id", referencedColumnName="id")
private UnitEntity units;
public DivisionEntity getDivision() {
return division;
}
public void setDivision(DivisionEntity division) {
this.division = division;
}
public UnitEntity getUnits() {
return units;
}
public void setUnits(UnitEntity units) {
this.units = units;
}
}
編輯:
我不得不手動設置divisionEntity和unitEntity:
@RequestMapping(value = "/addCard", method = RequestMethod.POST)
public String addCard(HttpServletRequest request, @ModelAttribute(value="card") CardEntity card, BindingResult result)
{
card.setDivision(divisionService.findOne(Integer.parseInt(request.getParameter("division"))));
card.setUnit(unitService.findOne(Integer.parseInt(request.getParameter("unit"))));
cardService.addCard(card);
return "redirect:/card";
}
所以我想這個問題是divisionEntity
和unitEntity
對象不卡設置提交表單後。
和你的問題是? – mithrandir 2015-03-19 09:56:42
如果我從下拉菜單中選擇一個分區或單元並提交表單,它應該將card_id和unit_of_qty_id保存在卡表中,但它們是空的。 – Naveen 2015-03-19 10:06:01