2017-04-26 36 views
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总是为空(其他字段很好)。

我在搜索并尝试一些解决方案,但它不工作。

回答

0

我想你的问题是你的表单中有一个错字:select。你正在定义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> 
+0

谢谢你,但它似乎没有工作,notification.listNotificationUser是一个空的ArrayList :( –

+0

我只注意到一个问题,你的用户列表存储对象类型的用户,但该属性listNotificationUser店NotificationUser,所以它不起作用,你需要使用相同类型的对象 – cralfaro