2017-04-26 3 views
0

나는 많은 관계를 가지고 컨트롤러의 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를 내가 컨트롤러에 POST 양식을 제출하면, 필드 notification.listNotificationUser 항상

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

다른 필드는 괜찮습니다.

검색을 시도했지만 해결책이 있지만 작동하지 않습니다.

답변

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