0
可以說我有2個實體,Dish和Ingrediënt。Spring web mvc實體類中的集合
大家都知道一個菜包括了多種成分的,所以我們說:
Dish.java:
@Entity
public class Dish {
@Id
@GeneratedValue
protected long id;
private String name;
@OneToMany(mappedBy = "dish", cascade = CascadeType.PERSIST)
private Collection<Ingredient> ingredients;
//getters & setters
}
Ingrediënt.java:
@Entity
public class Ingredient {
@Id
@GeneratedValue
protected long id;
private String name;
//getters &setters
}
如果我們再有DishController.java:
@Controller
public class DishController {
Service service;
public DishController() throws ServiceException {
service = new ShoppingFacade("JPA");
}
@RequestMapping("/showDishOverview")
protected ModelAndView getDishes() throws ServiceException {
Collection<Dish> dishes = service.getAllDishes();
return new ModelAndView("dish/dishOverview", "dishes", dishes);
}
@RequestMapping(value = "/showDishForm", method = RequestMethod.GET)
protected ModelAndView showDishForm(@RequestParam(value = "id") long dishId)throws ServiceException{
Dish dish = shoppingFacade.getDishById(dishId);
return new ModelAndView("dish/dishForm", "dish", dish);
}
@RequestMapping(value = "/editDish", method = RequestMethod.POST)
protected String updateDish(@ModelAttribute("dish") Dish newDish) throws ServiceException{
service.updateDish(newDish);
return "forward:/showDishOverview.htm";
}
}
現在讓我們說,我們選擇一盤由它的ID:
<a href="showDishForm.htm?id=${dish.getId()}">${dish.getName()}</a>
現在在我們的控制器中的showDishForm方法將被調用。 將我們送到dishForm.jsp:
<form method="post" action="editDish.htm">
<table>
<tr>
<td><label>Name </label></td>
<td><input type="text" name="name" placeholder="${dish.getName()}" value="${dish.getName()}"/></td>
</tr>
<tr>
<td><label>People</label></td>
<td><input type="text" name="people" placeholder="${dish.getPeople()}" value="${dish.getPeople()}"/></td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="ingredients" value="${dish.getIngredient()}" />
<button type="submit" name="id" value="${dish.getId()}">Save</button>
</td>
</tr>
</table>
</form>
如何將Spring MVC的傳遞從我的觀點到控制器的信息? 因爲我明白了,只要我不傳遞任何類別的工作(註釋掉<input type="hidden" name="ingredients" value="${dish.getIngredient()}" />
) 如果我試圖通過集合我得到的消息:
HTTP狀態400 - 由發送的請求。客戶端在語法上是 不正確
你可以把你的整個代碼表單提交沒有收集和收集,以協助找出問題? – Aeseir
完成...希望它有幫助 –