在 thymeleaf 模板中发布 OneToMany 数据库记录
Posting OneToMany database records in thymeleaf templates
我有 2 个 class 联系人和 Phone,class 联系人有一组 Phone 联系人。
这是我的控制器
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";
}
我想通过 thymeleaf 模板显示我的数据库记录,这是我的 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
Phone class - http://pastebin.com/L6Sqsp9q
联系人 class 有一个
@OneToMany(获取=FetchType.LAZY)
@JoinColumn(名字="contactId")
私有集 phones;
如何让我的控制器显示联系人 ID、姓名和一组 phone 号码?
你所拥有的是打印 Phone
本身,它解释了你的输出。您需要做的是访问 item
的 number
字段。例如,
th:each="item : ${contact.phones}" th:text="${item.number}"
或者,
th:each="item : ${contact.phones}" th:text="${item.getNumber()}"
我有 2 个 class 联系人和 Phone,class 联系人有一组 Phone 联系人。 这是我的控制器
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";
}
我想通过 thymeleaf 模板显示我的数据库记录,这是我的 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 Phone class - http://pastebin.com/L6Sqsp9q 联系人 class 有一个 @OneToMany(获取=FetchType.LAZY) @JoinColumn(名字="contactId") 私有集 phones;
如何让我的控制器显示联系人 ID、姓名和一组 phone 号码?
你所拥有的是打印 Phone
本身,它解释了你的输出。您需要做的是访问 item
的 number
字段。例如,
th:each="item : ${contact.phones}" th:text="${item.number}"
或者,
th:each="item : ${contact.phones}" th:text="${item.getNumber()}"