胸腺生成无查询参数的URL,但是?出现

发布于 2025-01-20 06:43:09 字数 1909 浏览 1 评论 0原文

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

thymeleaf 部分代码:

         <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 界面如下所示:

在此处输入图像描述

单击编辑按钮时(详细信息按钮也是如此),它加载正确,但 URL 为其他查询参数生成 ? 字符,但实际上没有(例如 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
}

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

I'm working on a simple Spring Boot MVC application using Thymeleaf.

Part of the thymeleaf code:

         <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>

The UI interface looks like this:

enter image description here

When clicking on the Edit button (same goes for the Details one), it loads correctly, but the URL generates the ? character for other query parameters, but there are actually none (for example http://localhost:8089/departments/3/edit?).

enter image description here

The controller code is:

@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
}

My problem is with the ? character that is being displayed. I don't want it to appear as there are no query parameters. Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一直都在从未离去 2025-01-27 06:43:09

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

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

It's adding a question ? because you are submitting an empty form. Why not just use a link styled as a button?

<a class="btn btn-secondary" th:href="@{/departments/{id}/edit(id=${dept.id})}" role="button">Edit</a>
最美不过初阳 2025-01-27 06:43:09

尝试

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

Try

<form th:object="${dept}" th:action="@{/departments/edit/{id}(id=${dept.getId()})}">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文