我想在 Thymeleaf 中創建一個鏈接以將用戶路由到編輯頁面。我的 Java 類看起來像這樣(沒有 getter 和 setter):
@Entity
@Table(name = "agencies")
public class Agency {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false)
private String name;
@Column(nullable = false, unique = true, length = 50)
private String email;
private String address;
}
我也有一個帶有這種方法的控制器:
@Autowired private AgencyService service;
@GetMapping("/agencies")
public String showAgencyList(Model model) {
List<Agency> agencies = service.listAll();
model.addAttribute("agencies", agencies);
return "agencies";
}
這是 Thymeleaf 中代碼的一部分:
<th:block th:each="agency : ${agencies}">
<tr>
<td>[[${agency.name}]]</td>
<td>[[${agency.email}]]</td>
<td>[[${agency.address}]]</td>
<td>
<a class="h4 mr-3" th:href="@{'/agencies/edit/' ${agency.id} }">Edit</a>
<a class="h4" th:href="@{/agents}">Choose</a>
</td>
</tr>
</th:block>
現在一切正常,除了在th:href="@{'/agencies/edit/' ${agency.id} }它說的路徑中無法決議代理,無法決議 id。這是為什么?該表已正確顯示,我無法弄清楚為什么在提供 Thymeleaf 中另一個視圖的路徑時,代理似乎未知。
uj5u.com熱心網友回復:
${agency.id} }創建問題之間的空白。
改變
th:href="@{'/agencies/edit/' ${agency.id} }"
到
th:href="@{'/agencies/edit/' ${agency.id}}"
或者
th:href="@{'/agencies/edit/{id}(id = ${agency.id})'}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/345314.html
