当前位置: 首页 > 知识库问答 >
问题:

将用户分配给会议。字符串与集合类型不匹配的问题

贲宏硕
2023-03-14

我有一个应用程序,其中我正在尝试分配用户到一个会议。但当提交时,它会作为字符串传递,而我会收到一个类型不匹配的错误。

我的控制器方法:

@GetMapping(path = "/assign/{id}")
public String showStudentAssignForm(@PathVariable(name = "id") Long id, Model model){
    Optional<Conference> conference = conferenceService.findById(id);
    List<User> userList = userService.findAllByRoles("ROLE_STUDENT");
    model.addAttribute("userList", userList);
    model.addAttribute("conference", conference.get());
    return "assignStudent";
}

@PostMapping(path = "/assign/save")
public String saveAssignedUsers(ConferenceDto conference){
    conferenceService.updateConference(conference);
    return "redirect:/teacher/configure";
}
private Collection<User> students;
            <div class="form-group row">
                <label class="col-form-label col-sm-4">Users: </label>
                <div class="col-sm-8 text-left">
                    <th:block th:each="user : ${userList}">
                        <div>
                            <input type="checkbox" th:field="*{students}" th:text="${user.name}" th:value="${user}" class="m-2" />
                        </div>
                    </th:block>
                </div>
            </div>
Field error in object 'conferenceDto' on field 'students': rejected value [Bill Gates,Test McTest]; codes [typeMismatch.conferenceDto.students,typeMismatch.students,typeMismatch.java.util.Collection,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [conferenceDto.students,students]; arguments []; default message [students]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.Collection' for property 'students'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.lukas.ramonas.cms.Model.User' for property 'students[0]': no matching editors or conversion strategy found]]

共有1个答案

房星光
2023-03-14

所以问题似乎是围绕字符串与集合和/或用户不匹配而产生的。通过将conferenceDto中的Private Collection Students; 更改为Private Collection Students; 来解决此问题,然后保存到ConferenceService类中时,我将从conferenceDto中的字符串中找到的具有findByName或findById的用户列表添加到会议(model对象,而不是Dto)中。

 类似资料: