Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring boot save将学生分配给组_Java_Spring_Spring Boot_Thymeleaf - Fatal编程技术网

Java Spring boot save将学生分配给组

Java Spring boot save将学生分配给组,java,spring,spring-boot,thymeleaf,Java,Spring,Spring Boot,Thymeleaf,我的spring boot简单应用程序有问题。我写了一本学生日记。我有分配学生到小组的功能。我在thymeleaf视图中创建,如下所示: 怀比尔兹·格鲁普: 威比尔兹学生会: 我的模型类用户和组: 组: 这是我的控制器: @RequestMapping(value = "/showGroupList", method = RequestMethod.GET) public String showGroupList(Model model){ List<Group> group

我的spring boot简单应用程序有问题。我写了一本学生日记。我有分配学生到小组的功能。我在thymeleaf视图中创建,如下所示:

怀比尔兹·格鲁普: 威比尔兹学生会: 我的模型类用户和组:

组:

这是我的控制器:

@RequestMapping(value = "/showGroupList", method = RequestMethod.GET)
public String showGroupList(Model model){
    List<Group> groupList = groupRepository.findAll();
    List<User> userList = userRepository.findAll();
    Role userRole = roleRepository.findByName("ROLE_USER");
    List<User> userListNew = new ArrayList<>();

    for (User user : userList){
        if (user.getRoles().contains(userRole)){
            userListNew.add(user);
        }
    }

    model.addAttribute("groupList", groupList);
    model.addAttribute("userListNew", userListNew);
    return "groupList";
}
现在,当我从他们的列表中选择group and user并单击submit时,出现以下错误:

警告1132-[nio-8070-exec-7].w.s.m.s.DefaultHandlerExceptionResolver:已解决[org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“POST]


我做错了什么?

这是您希望在表单中使用的方法的问题。您的错误是:

Request method 'POST' not supported
它清楚地表明,您的应用程序不知道如何处理Post请求,而您要求发布表单。在表单顶部,您要求使用POST http方法:

<form name="myForm" method="post">
method = RequestMethod.GET
您可以通过执行以下两个选项之一来解决此问题:更改要获取的表单,或在控制器中指定发送POST请求时要执行的操作。看看你的申请表,我猜你想先打电话给GET获取列表,然后在提交时发布——但这是一个你更喜欢的设计问题

为了添加一个POST控制器,可能类似这样的操作会起作用:

@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity <String> persistGroup(@RequestBody Group group) {
    groupRepository.save(group);
    return ResponseEntity.status(HttpStatus.CREATED).build();
}

您将需要使用特定的实现进行测试,以查看表单作为一个主体所采用的内容—它可能不是一个组—并查看它指向/可能不是正确的URI。想法是将组或用户保存到各自的存储库中。

首先,您需要HTML中的一个表单,以便发送帖子,或者您可以使用jQuery或JS发送帖子,但这会增加代码的复杂性。添加表单后,您将需要添加一个方法来捕获该请求

HTML


如果您还需要什么,请告诉我。

是的,在我的项目中,首先我希望获得所有没有团队的团队和学生。我想把它保存到数据库里。I我的用户表I有列组_id,但现在为空。如果是这种情况,您将需要实现两个更改:在控制器中指定一个POST处理程序,以便处理最终的POST;并且在你的表单中添加一个get来检索列表。“麦特,如果你觉得我回答了你的问题,你会考虑通过点击左边的灰色复选标记来接受它,使它变成绿色吗?好吧,那么,后控制器看起来像什么,会把一个学生分配给一个组??我将把它添加到答案中。
method = RequestMethod.GET
@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity <String> persistGroup(@RequestBody Group group) {
    groupRepository.save(group);
    return ResponseEntity.status(HttpStatus.CREATED).build();
}
<form th:action="@{/assign-group}" th:object="${newUser}" method="post">
    <select class="form-control" id="dropGroup" th:field="*{group.id}">
        <option value="0">Wybierz grupę : </option>
        <option th:each="selectedGroup : ${groupList}" th:value="${selectedGroup.id}" th:text="${group.name}">
        </option>
    </select>
    <select class="form-control" id="dropGroup" th:field="*{id}">
        <option value="0">Wybierz studenta : </option>
        <option th:each="selectedUser : ${userList}" th:value="${selectedUser.id}" th:text="${user.name}">
        </option>
    </select>
    <input name="submit" type="submit" value="submit" />
</form>
@ModelAttribute(value = "newUser")
public Store newUser() {return new User();}

@RequestMapping(value = "/assign-group", method = RequestMethod.POST)
public String assignGroup(@ModelAttribute("newUser") User user) {
    // Fetch your the rest of the information using the id received.
    // Fetch the group using the group's id received.
    // Set this group to the fetched user.
}