胸腺胸骨不做对象的打印列表(Spring JPA)
我有5个不同的课堂游览对象。我希望我的主页显示了带有其字段的对象。 写了conrtoller:
@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:/";}
}
repository
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
我
<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>
和 如何更改控制器\ view?
I have 5 different object of class Tour with some fields.I want that my main page shows this object with their fields.
I wrote conrtoller:
@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:/";}
}
And Repository
public interface TourRepository extends JpaRepository<Tour,Integer> {
}
application.properties:
# hibernate configurations
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# thumeleaf configurations
spring.thymeleaf.mode= HTML
spring.thymeleaf.cache=false
Now I'm trying to pass variable values to the page with 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>
but I get five identical duplicates of first record in db.
How can I change controller\view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的主要键在DB中的实体Tout中名为“代码”。注释
@id
没有用不同名称作为主键的列。我明确添加了:My primary key named "code" in entity tout in db. Annotation
@Id
didn't a column with a different name as a primary key. I explicitly added: