Thymeleaf 生成 URL 没有查询参数,但是 '?'出现

Thymeleaf generates URL with no query parameters, but '?' appears

我正在使用 Thymeleaf 开发一个简单的 Spring Boot MVC application

百里香部分代码:

         <tr th:each="dept: ${departments}">

        <td th:text="${dept.getId()}"></td>
        <td th:text="${dept.getName()}"></td>
        <td>
            <form th:object="${dept}" th:action="@{'/departments/' + ${dept.getId()}}">
                <button class="btn btn-secondary">Details</button>
            </form>
        </td>

        <td>
            <form th:object="${dept}" th:action="@{/departments/{id}/edit(id=${dept.getId()})}">
                <button class="btn btn-secondary">Edit</button>
            </form>
        </td>
        <td>
            <form th:object="${dept}" th:action="@{/departments/{id}(id=${dept.getId()})}" th:method="delete">
                <button class="btn btn-secondary">Delete</button>
            </form>
        </td>
    </tr>

UI 界面如下所示:

单击 Edit 按钮时(Details 按钮也是如此),它会正确加载,但 URL 会为其他查询生成 ? 字符参数,但实际上有none(例如http://localhost:8089/departments/3/edit?)。

控制器代码为:

@GetMapping("/{id}/edit")
public String editDepartment(@PathVariable("id") String departmentId, Model model) {
    var department = departmentService.getDepartmentById(Long.valueOf(departmentId));
    model.addAttribute("department", department);
    return "edit-department"; // returns view
}

我的问题是显示的 ? 字符。我不希望它出现,因为没有查询参数。有帮助吗?

尝试

<form th:object="${dept}" th:action="@{/departments/edit/{id}(id=${dept.getId()})}">

它正在添加一个问题 ? 因为您提交的是空表单。为什么不直接使用样式为按钮的 link?

<a class="btn btn-secondary" th:href="@{/departments/{id}/edit(id=${dept.id})}" role="button">Edit</a>