【发布时间】:2016-02-03 20:08:52
【问题描述】:
我有 2 个类 - Contact 和 Phone,Contact 类有一个 Set of Phones 。 这是我的控制器
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 电话课-http://pastebin.com/L6Sqsp9q 接触类有一个 @OneToMany(fetch = FetchType.LAZY) @JoinColumn(name = "contactId") 私人设置电话;
如何让我的控制器显示联系人 ID、姓名和电话号码集?
【问题讨论】:
-
@ChiefTwoPencils 我不知道该怎么做,我对 thymeleaf 和 html 完全是菜鸟。虽然我认为我必须以某种方式访问电话类上的数字字段,但我不知道如何
-
忽略最后一条评论,我看不出问题出在哪里。