2017-09-30 56 views
0

我有一個彈簧啓動應用程序,以及 我試圖用POST方法下面的控制器發送對象:請求方法「POST」不支持

@PostMapping("/suggestevent") 
    public String receiveSuggestedEvent(@ModelAttribute SuggestedEvent event) { 

     return "redirect:/suggestEvent"; 
    } 

,但它與抱怨:

There was an unexpected error (type=Method Not Allowed, status=405). 
Request method 'POST' not supported 

那麼,什麼是錯的?

更新: 我已經試過這一點,它也沒有工作

@RequestMapping(value = "/suggestevent", method = RequestMethod.POST) 

表單包含一些簡單的inputes和選擇,其工作原理基於Thyemeleaf。這裏是我的形式:

<form th:action="@{/suggestevent}" method="post"> 

       <div class="form-group"> 
        <label for="title">Title</label> 
        <input type="text" 
         class="form-control" id="title" placeholder="Event title" 
         th:value="${event.title}" 
         name="title" required="required"/> 
       </div> 

       <div class="form-group"> 
        <label for="mainFouce">Main Focus</label> 
        <input type="text" 
         class="form-control" id="Focus" placeholder="Focus" 
         th:value="${event.mainFouce}" 
         name="Focus" required="required"/> 
       </div> 

       Event Type 
       <div class="form-group"> 
        <select class="form-control" name="type" th:value="${event.type}"> 
         <option value="volvo">Party</option> 
         <option value="saab">Workshop</option> 
         <option value="fiat">Friendship</option> 
        </select> 
       </div> 

       <div class="form-group"> 
        <label for="city">Area</label> 
        <input type="text" 
         class="form-control" id="area" 
         th:value="${event.area}" 
         placeholder="Berlin, Estonia ,or even Asia" name="area" 
         required="required"/> 
       </div> 

       <div class="form-group"> 
        <label for="description">Description</label> 
        <textarea name="description" class="form-control" 
         th:value="${event.description}" 
         required="required" form="usrform" 
         placeholder="What makes it an special event?"></textarea> 
       </div> 

       <button type="submit" class="btn btn-default">Submit</button> 
      </form> 

的發送對象是:

@Entity 
@Data 
public class SuggestedEvent { 

    @Id 
    @GeneratedValue 
    Long id; 

    String title; 
    String mainFouce; 
    EventType type; 
    String area; 
    String description; 

} 

郵遞員可以順利到達控制器,但thyemeleaf抱怨!

+0

可能有多種原因。你檢查了[這些問題](https://stackoverflow.com/search?q=Request+method+%27POST%27+not+supported)?似乎有很多人有同樣的問題。 – mumpitz

+0

如果您使用CSRF保護,請添加輸入字段:https://stackoverflow.com/questions/11145884/http-status-405-request-method-post-not-supported-spring-mvc – bphilipnyc

回答

0

當您嘗試添加表格內的空SuggestedEvent對象,並嘗試填充這樣的對象,因此,會發生什麼:

<form th:action="@{/suggestevent}" th:object="${event}" method="post"> 
       <div class="form-group"> 
        <label for="title">Title</label> 
        <input type="text" 
         class="form-control" id="title" placeholder="Event title" 
         th:value="*{title}" 
         name="title" required="required"/> 
       </div> 

       <div class="form-group"> 
        <label for="mainFouce">Main Focus</label> 
        <input type="text" 
         class="form-control" id="Focus" placeholder="Focus" 
         th:value="*{mainFouce}" 
         name="Focus" required="required"/> 
       </div> 

       Event Type 
       <div class="form-group"> 
        <select class="form-control" name="type" th:value="*{type}"> 
         <option value="volvo">Party</option> 
         <option value="saab">Workshop</option> 
         <option value="fiat">Friendship</option> 
        </select> 
       </div> 

       <div class="form-group"> 
        <label for="city">Area</label> 
        <input type="text" 
         class="form-control" id="area" 
         th:value="*{area}" 
         placeholder="Berlin, Estonia ,or even Asia" name="area" 
         required="required"/> 
       </div> 

       <div class="form-group"> 
        <label for="description">Description</label> 
        <textarea name="description" class="form-control" 
         th:value="*{description}" 
         required="required" form="usrform" 
         placeholder="What makes it an special event?"></textarea> 
       </div> 

    <button class="btn btn-default">Submit</button> 
</form> 

而在@GetMapping("/suggestEvent")

@GetMapping("/suggestEvent") 
    public String getSuggestEventPage(Model model) { 
    model.addAttribute("event", new SuggestEvent()); 

    return "suggestEventPage"; 
    } 

時退房官方Thymeleaf doc否則。這應該不是一個問題,因爲我在my project中完全一樣。

http://www.thymeleaf.org/doc/articles/standarddialect5minutes.html http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html http://www.thymeleaf.org/documentation.html

相關問題