请求方法“POST”不受支持

时间:2017-09-30 10:46:09

标签: spring-boot controller thymeleaf

我有一个spring-Boot应用程序,并且 我正在尝试使用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)

表单包含一些简单的输入,以及一个基于Thyemeleaf工作的select。这是我的表格:

<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抱怨!!!

1 个答案:

答案 0 :(得分: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