我无法通过控制器传输很多对象(((

Posted

技术标签:

【中文标题】我无法通过控制器传输很多对象(((【英文标题】:I can’t transfer many objects through the controller((( 【发布时间】:2020-08-07 15:30:36 【问题描述】:

当我尝试通过控制器传输大量数据时,出现错误:

出现意外错误(类型=错误请求,状态=400)。 无法将类型“java.lang.String”的值转换为所需类型“com.psu.projectmethod.domain.wrappers.UserSet”;嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [@org.springframework.web.bind.annotation.ModelAttribute com.psu.projectmethod.domain.wrappers。 UserSet] 为值 'com.psu.projectmethod.domain.User@23';嵌套异常是 java.lang.IllegalArgumentException:无法实例化集合类型:com.psu.projectmethod.domain.wrappers.UserSet org.springframework.beans.TypeMismatchException:无法将类型“java.lang.String”的值转换为所需类型“com.psu.projectmethod.domain.wrappers.UserSet”;嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [@org.springframework.web.bind.annotation.ModelAttribute com.psu.projectmethod.domain.wrappers。 UserSet] 为值 'com.psu.projectmethod.domain.User@23';嵌套异常是 java.lang.IllegalArgumentException:无法实例化集合类型:com.psu.projectmethod.domain.wrappers.UserSet

我根据 *** 中的示例和这篇文章 https://www.viralpatel.net/spring-mvc-multi-row-submit-java-list/ 做了所有事情。请帮我解决这个问题((

我的控制器方法:

@PreAuthorize("hasAuthority('_2_TEACHER')")
@GetMapping("/project/projectId/party/create")
public String viewTeacherCreateProjectParty(
        @PathVariable("projectId") Project project,
        Party party,
        Model model) 
    Set<User> users = userService.userList(Sort.by("username"));
    UserSet userSet = new UserSet(users);
    model.addAttribute("project", project);
    model.addAttribute("party", party);
    model.addAttribute("users", userSet.getUsers());
    return "teacherProjectPartyCreate";


@PreAuthorize("hasAuthority('_2_TEACHER')")
@PostMapping("/project/projectId/party/create")
public String processCreateProjectParty(
        @PathVariable("projectId") Project project,
        @Valid Party party,
        @ModelAttribute("users") UserSet users,
        Model model) 
    model.addAttribute("project", project);
    model.addAttribute("party", party);
    Long projectId = projectService.createProjectParty(project, party, users);
    return "redirect:/projects/project/" + projectId;

Freemarker 表单:

<form action="/projects/project/$project.projectId/party/create" method="post"
                          style="color: #757575;">

                        <!-- CSRF Token -->
                        <input type="hidden" name="_csrf" value="$_csrf.token"/>

                        <div class="md-form">
                            <input type="text" id="partyName" name="partyName" value="$party.partyName!''"
                                   class="form-control $(partyNameError??)?string('is-invalid', '')" required>
                            <label for="partyName">Group name</label>
                            <#if partyNameError??>
                                <div class="invalid-feedback">
                                    $partyNameError
                                </div>
                            </#if>
                        </div>

                        <select name="users" class="selectpicker"
                                data-header="Select users"
                                data-live-search="true"
                                data-selected-text-format="count"
                                data-size="auto"
                                data-style="btn-unique"
                                data-
                                multiple
                                title="Select users"
                        >
                            <#list users as user>
                                <option value="$user">$user.fullname</option>
                            </#list>
                        </select>

                        <div class="modal-footer d-flex justify-content-center">

                            <button type="button" onClick='location.href="/projects/project/$project.projectId"'
                                    class="btn btn-outline-info waves-effect">
                                Отмена
                            </button>

                            <button type="submit" class="btn btn-primary waves-effect">
                                Save
                            </button>

                        </div>

                    </form>

我的实体创建方法

public Long createProjectParty(Project project, Party party, Set<User> users) 
    project.addParty(party);
    party.addUsers(party, users);
    projectRepo.save(project);
    return project.getProjectId();

添加双向关联的辅助方法:

public void addParty(Party party) 
    party.setPartyProject(this);
    this.projectParties.add(party);


public void addUsers(Party party, Set<User> users) 
    party.setPartyUsers(users);
    this.partyUsers.addAll(users);

ManyToMany 关联映射:

@ManyToMany(fetch = FetchType.LAZY,
        cascade = CascadeType.PERSIST, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.REMOVE
)
@JoinTable(name = "party_users",
        joinColumns = @JoinColumn(name = "fk_party_id"),
        inverseJoinColumns = @JoinColumn(name = "fk_user_id"))
private Set<User> partyUsers = new HashSet<>();

public class UserSet implements Set<User> 
private Set<User> users;

public UserSet(Set<User> users) 
    this.users = users;


public Set<User> getUsers() 
    return users;


public void setUsers(Set<User> users) 
    this.users = users;

... // getters, setters and Set metods 

【问题讨论】:

我会先将 1 或 2 个对象发送到视图,然后慢慢添加下一个,直到您在此处隔离问题。 【参考方案1】:

分析您的日志后,我发现您的请求有问题。

仔细阅读此日志条目:

Failed to convert value of type 'java.lang.String' to required type 'com.psu.projectmethod.domain.wrappers.UserSet'

您似乎正在尝试将String 发送到预期UserSet 的位置。如果需要这种行为,请考虑使用转换器。检查article

【讨论】:

以上是关于我无法通过控制器传输很多对象(((的主要内容,如果未能解决你的问题,请参考以下文章

设计模式-传输对象模式

Xcode(Swift):我无法在视图控制器之间传输数据

Spring 控制器方法的返回值类型ModelAndView当中页面当中数据的传输(通过转发和重定向)

Spring 控制器方法的返回值类型ModelAndView当中页面当中数据的传输(通过转发和重定向)

Spring 控制器方法的返回值类型ModelAndView当中页面当中数据的传输(通过转发和重定向)

将对象从 Realm 数据库传输到另一个 UITabBar 控制器