我正在做一個簡單的Spring Boot MVC application使用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 界面如下所示:

單擊Edit按鈕時(按鈕也是如此Details),它會正確加載,但 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
}
我的問題是?正在顯示的字符。我不希望它出現,因為沒有查詢引數。有什么幫助嗎?
uj5u.com熱心網友回復:
它正在添加一個問題?,因為您正在提交一個空表單。為什么不直接使用按鈕樣式的鏈接?
<a class="btn btn-secondary" th:href="@{/departments/{id}/edit(id=${dept.id})}" role="button">Edit</a>
uj5u.com熱心網友回復:
嘗試
<form th:object="${dept}" th:action="@{/departments/edit/{id}(id=${dept.getId()})}">
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/461382.html
