2017-04-26 11 views
0

私はオブジェクトのコントローラ上でPOSTフォームデータを取得しようとしていますが、1対多の関係を持っています。コードJavaスプリングポストフォームと1対多の関係

マイ通知モデル

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> 

以下のように私はいつも、コントローラにフィールドnotification.listNotificationUserをPOSTフォームを送信するとnullです(他のフィールドは正常です)。

私は検索して解決策を試しましたが、機能しません。

答えて

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> 
+0

ようにする必要があり、私はちょうど問題に気づいありがとう、それが動作していないように見える、notification.listNotificationUserが空のArrayListです:( –

+0

、ユーザーリストストアは、タイプのユーザーが、属性listNotificationUser店NotificationUserオブジェクトそのように動作しないので、同じ種類のオブジェクトを使用する必要があります – cralfaro