本文介紹了在胸腺葉模板中發布OneToMany數據庫記錄的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有兩個類-聯系人和電話,類聯系人有一組電話。
這是礦井控制器
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class ContactFormController {
@Autowired
ContactRepository contactRepo;
@Autowired
PhoneRepository phoneRepo;
@RequestMapping(value = "/data", method = RequestMethod.GET)
public String showAll(Model model) {
model.addAttribute("contacts", contactRepo.findAll());
model.addAttribute("phones", phoneRepo.findAll());
return "dataresult";
}
我想通過胸腺葉模板顯示我的數據庫記錄,下面是我的html代碼:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Dane</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>Dane</h3>
<p th:each="contact : ${contacts}">
<h4>ID:</h4>
<div th:text="${contact.id}"></div>
<h4>Name:</h4>
<div th:text="${contact.firstName}"></div>
<li th:each="item : ${contact.phones}" th:text="${item}">Item description here...</li>
<div>---------</div>
</p>
</body>
</html>
以下是我得到的結果-http://i.imgur.com/pPIA5ma.png
電話類-http://pastebin.com/L6Sqsp9q
Contact類有一個
@OneToMany(FETCH=FetchType.LAZY)
@JoinColumn(name=”contactId”)
私人固定電話;
如何使我的控制器顯示聯系人ID、姓名和電話號碼集?
推薦答案
這里打印的是解釋輸出的Phone
本身。您需要做的是訪問item
的number
字段。例如,
th:each="item : ${contact.phones}" th:text="${item.number}"
或
th:each="item : ${contact.phones}" th:text="${item.getNumber()}"
這篇關于在胸腺葉模板中發布OneToMany數據庫記錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,