【问题标题】:Cant change column value by pressing button (Spring Boot, Thymeleaf)无法通过按下按钮更改列值(Spring Boot,Thymeleaf)
【发布时间】:2021-04-12 15:43:56
【问题描述】:

我每次按下按钮都会收到Request method 'POST' not supported error

EventController.java 片段

@PostMapping(value = "/ledger/{id}/get-place")
public String modifyLedger(@PathVariable(value = "id") long id, @RequestParam Long place, Model model) {
    Event event = eventRepository.findById(id).orElseThrow();
    event.setPlace(place - 1);
    eventRepository.save(event);
    return "ledgerInfo";
}

该函数假设每次按下按钮时都会从 Long place 变量中减去 1,但每次出现 HttpRequestMethodNotSupportedException 错误时

分类帐信息.html

<div class = "container mt-5">
    <h1>Ledger Info</h1>
    <div th:each="elem:${event}" class="alert alert-info mt-2">
        <h2 th:text="${elem.title}"/>
        Line count:<p th:text="${elem.line}"/>
        Place remain:<p th:text="${elem.place}"/>
        Place status:<p th:text="${elem.status}"/>
        <form method="post">
            <button type="submit" class="btn btn-success">Get Place</button>
        </form>
    </div>
</div>
不知道如何解决这个问题。有什么线索吗?

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案1】:

    您没有为表单指定操作。 在表单标签中使用 th:action="@{/your Url}"。

    【讨论】:

    • 它的作品,但现在我按下按钮时遇到另一个问题:Required Long parameter 'place' is not present
    • 你的控制器方法有@RequestParam Long place,这意味着place请求参数必须出现在你使用的URL中(例如http://...?place=something)
    猜你喜欢
    • 1970-01-01
    • 2014-04-02
    • 2021-10-24
    • 2021-05-28
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多