我有一個帶有方法的控制器:
@GetMapping("/showProducts")
public String returnAllProductsInATable(Model model) {
model.addAttribute("products", listProducts)
return "showProducts"
}
showProduct 是模板檔案夾中的一個頁面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>Список товаров</title>
</head>
<body>
<table>
<tr th:each="product: ${products}">
<td th:text="${product.id}"></td>
<td th:text="${product.productSeries}"></td>
<td th:text="${product.name}"></td>
<td th:text="${product.price}"></td>
<td th:text="${product.weight}"></td>
<td th:text="${product.description}"></td>
</tr>
</table>
</body>
</html>
控制器類使用 @RestController 進行注釋,包括 spring-boot-starter-thymeleaf、spring-boot-starter-web 和 spring-webmvc 的依賴項,但我看到的不是模型“showProducts”字串,這是為什么呢?
uj5u.com熱心網友回復:
因為你使用@Restcontroller
- @restcontroller 用于創建 RESTAPI g?m các ph??ng th?c nh?:GetMapping、PostMapping、PutMapping、DeleteMapping ...
- @Controller 用于處理和接收請求
uj5u.com熱心網友回復:
答案是@RestController 包含@ResponseBody 注釋,我只需要使用@Controller 注釋代替
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/508396.html