我有 5 個不同的類 Tour 物件和一些欄位。我希望我的主頁顯示這個物件及其欄位。我寫了控制器:
@Controller
public class MainController {
@Autowired
private TourRepository tourRepository;
@GetMapping("/")
public String mainPage(Model model) {
List<Tour> tourList=tourRepository.findAll();
model.addAttribute("tours",tourList);
return "home/main";
}
@RequestMapping("/main")
public String main() {
return "redirect:/";}
}
和存盤庫
public interface TourRepository extends JpaRepository<Tour,Integer> {
}
應用程式屬性:
# hibernate configurations
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# thumeleaf configurations
spring.thymeleaf.mode= HTML
spring.thymeleaf.cache=false
現在我正在嘗試使用 Thymeleaf 將變數值傳遞給頁面,
<div>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr th:each ="tour : ${tours}">
<td th:utext="${tour.title}">...</td>
<td th:utext="${tour.price}">...</td>
</tr>
</tbody>
</table>
</div>
但我在 db 中得到了第一條記錄的五個相同副本。如何更改控制器\視圖?
uj5u.com熱心網友回復:
我在 db 中物體 tout 中名為“code”的主鍵。注釋@Id沒有將具有不同名稱的列作為主鍵。我明確添加:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "code")
private int id;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/477373.html
上一篇:來自控制器的html的PHP變數系結問題,用于ajax回應
下一篇:在JUnit5中,為什么postMethod會拋出TransactionSystemException而不是回傳帶有錯誤請求的ResponseEntity?
