0
我試圖讓控件的POST形式數據對象有一對多的關係。如下面的代碼Java彈簧帖子形成一對多關係
我通知模型
private long id;
@NotBlank(message = ERROR.NOT_EMPTY)
private String title;
@NotBlank(message = ERROR.NOT_EMPTY)
private String content;
// One Notification has many User
private List<NotificationUser> listNotificationUser;;
// Getter and setter method
我控制器
@RequestMapping(value = "notification/add", method = RequestMethod.GET)
public String create(ModelMap model) {
ArrayList<User> listUser = userService.getAllUsername();
model.put("user", listUser);
model.addAttribute("notification", new Notification());
return "notification/add";
}
@RequestMapping(value = "notification/add", method = RequestMethod.POST)
public String create(ModelMap model, HttpServletRequest request,
@Valid @ModelAttribute(value = "notification") Notification notification, BindingResult bindingResult) {
....
}
我的.jsp
<form:form method="POST" action="add" name="addForm" commandName="notification">
<!-- Other input -->
<form:select path="listNotificationUser" multiple="multiple" id="name" name="itemValue">
<form:options />
<form:options items="${user}" itemValue="id" itemLabel="name" />
</form:select>
<!-- Other input -->
</form:form>
當我提交表單POST到控制器,現場notification.listNotificationUser總是爲空(其他字段很好)。
我在搜索並嘗試一些解決方案,但它不工作。
謝謝你,但它似乎沒有工作,notification.listNotificationUser是一個空的ArrayList :( –
我只注意到一個問題,你的用戶列表存儲對象類型的用戶,但該屬性listNotificationUser店NotificationUser,所以它不起作用,你需要使用相同類型的對象 – cralfaro