我试图在对象的控制器上获取POST表单数据有一对多的关系。如下面的代码
我的通知模型
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始终为null(其他字段很好)。
我正在搜索并尝试一些解决方案,但它无效。
答案 0 :(得分:0)
我想你的问题是你的形式有错字:选择。您正在定义2个选项块,我想您只想定义一个空选项。所以应该像这样
<form:select path="listNotificationUser" multiple="multiple" id="name" name="itemValue">
<!-- Some value you can identify as empty option-->
<form:option value="0" label="--Options--"/>
<form:options items="${user}" itemValue="id" itemLabel="name" />
</form:select>