【问题标题】:Spring Boot:Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]Spring Boot:已解决 [org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“POST”]
【发布时间】:2020-07-03 07:21:15
【问题描述】:

我正在处理我的“Edit.html”。 此页面上有一个表单,该页面用于编辑现有页面上的信息。 我对许多网站都使用了相同的“发布”方法并且没有错误。 但我不知道为什么我此时会出现以下错误。

我的控制器:

    @PostMapping("editKursInfo")
    public String editKursInfo(
        @Valid @ModelAttribute("edit") Kurs kurs) 
    {
        data.saveKurs(kurs);
        return "redirect:/editKursInfo";
    }

HTML (为了看起来方便,我删除了相关的HTML布局代码):

           <form action="editKursInfo" method="POST" th:object="${edit}">
           <span th:text="${kurs.kursName}" id="kursNameSpan" style=" display: none;"></span>                          
           <td align="left" >Kurs Name</td>
           <td>
           <input th:field="*{kursName}"                                   
           type="text"
           align="left"                                
           class="form-control"
           style="width:250px;"   
           placeholder="Bitte geben Sie Ihren Vornamen ein"
           th:errorclass="is-invalid"
           id="kursNameInput"
           value=""/><br>
           <div th:if="${#fields.hasErrors('kursName')}" th:errors="*{kursName}" class="invalid-feedback">          </div>                                  
           </form>

错误:

Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

【问题讨论】:

    标签: html spring-boot thymeleaf


    【解决方案1】:

    您的端点 editKursInfo 必须同时支持 post 和 get(由于重定向)

    @RequestMapping(value="/editKursInfo", method = { RequestMethod.POST, RequestMethod.GET })
    

    或者您可以将响应状态设置为 307

     public String editKursInfo(
            @Valid @ModelAttribute("edit") Kurs kurs, HttpServletRequest request)    
     request.setAttribute(
          View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
    

    【讨论】:

    • 我已经有 GET 方法:@GetMapping("/editKursInfo/{id}") 现在 POST 有问题
    • 那么你的重定向不好,因为 de {id} 必须存在,否则它将转售到帖子
    • 也许 id 必须来自 kurs 的保存然后你可以这样做Kurs savedKurs=data.saveKurs(kurs); return "redirect:/editKursInfo/"+savedKurs.getId();
    • 还要注意相对路径@PostMapping("editKursInfo")@PostMapping("/editKursInfo") 你的类@RequestMapping 是什么?
    猜你喜欢
    • 2022-01-15
    • 2020-05-19
    • 2020-10-29
    • 2021-09-20
    • 2020-12-23
    • 2022-01-06
    • 2018-06-03
    • 2020-03-31
    • 2016-02-21
    相关资源
    最近更新 更多