spring Sping Boot 控制器未使用 AJAX POST更新数据库

z0qdvdin  于 2023-01-24  发布在  Spring
关注(0)|答案(1)|浏览(130)

所以我使用jQuery发送POST请求,每当表单的下拉部分的值发生变化,一切正常,除了票没有得到更新的数据库,当我重新加载页面,旧的值再次出现。我已经确认它得到正确的状态和优先级与日志,但由于某种原因没有更新数据库,并且此方法工作service.updateTicket(existentTicket);(由另一个@PostMapping确认,该@PostMapping使用相同的代码行并更新DB,没有问题)
控制器:

@PostMapping("/tickets/details/{id}")
@ResponseBody
public String updateTicketFromDetailsForm(@PathVariable Long id, @RequestParam Integer statusId, @RequestParam Integer priorityId) {
    TicketEntity existentTicket = service.findTicketById(id);

    existentTicket.setStatus(service.findStatusById(statusId));
    existentTicket.setPriority(service.findPriorityById(priorityId));

    LOGGER.warn(existentTicket.getStatus().getName());
    LOGGER.warn(existentTicket.getPriority().getName());

    service.updateTicket(existentTicket);

    return "Success";

}

表格:

<form class="list-group list-group-flush" id="ticketDetailsForm">
                <li th:text="'Ticket ID: ' + ${ticket.getId()}" class="list-group-item">Ticket ID</li>
                <li th:text="'Created At: ' + ${ticket.getCreatedAtFormatted()}" class="list-group-item">Created
                    At
                </li>
                <li class="list-group-item">
                    <label class="ms-0">Status: </label>
                    <select class="d-inline dropdown-menu bg-gradient-info text-white text-center p-1 ms-2"
                            id="statusSelect"
                            required>
                        <option class="text-body" value="" disabled>Please select</option>
                        <option class="text-body" th:each="status : ${statuses}" id="statusOption"
                                th:value="${status.getId()}"
                                th:text="${status.getName()}"></option>
                    </select>
                </li>
                <li class="list-group-item">
                    <label class="ms-0">Priority: </label>
                    <select
                            class="d-inline dropdown-menu bg-gradient-info text-white text-center p-1 ms-2"
                            id="prioritySelect" required>
                        <option class="text-body" value="" disabled>Please select</option>
                        <option class="text-body" th:each="prio : ${priorities}" id="priorityOption"
                                th:value="${prio.getId()}"
                                th:text="${prio.name}"></option>
                    </select>
                </li>
            </form>

Screnshot of the form
下面是jQuery:

$("#ticketDetailsForm").on("change", function () {
    $.ajax({
        method: "POST",
        url: $(location).attr('pathname'),
        data: {
            "statusId": $("#statusSelect").val(),
            "priorityId": $("#prioritySelect").val()
        }
    })
});

不知道我做错了什么,任何帮助都将不胜感激:)

rjee0c15

rjee0c151#

我想明白了,我只是错过了<form> y中的th:object="${ticket}"<select>中的th:field="*{status}"(也是优先级1)。控制器和jQuery实际上是工作的。

相关问题