0
我想包括視圖服務已經從我的數據庫檢索數據到我的index.html頁面。我正在使用春天和Thymeleaf。UseThymeleaf模板視圖與從數據庫檢索數據到另一個模板視圖
這是我的服務控制器
@Controller
public class MyServicesController{
@Autowired
private ServicesForOurServices servicesForOurServices;
@RequestMapping(value = "myservices",method = RequestMethod.GET)
public String myServices(Model model){
model.addAttribute("services",servicesForOurServices.listAll());
return "services";
}
@RequestMapping(value = "services",method = RequestMethod.GET)
public ModelAndView listAll() {
ModelAndView modelAndView = new ModelAndView("services");
modelAndView.addObject("services",servicesForOurServices.listAll());
return modelAndView;
}
@ModelAttribute("services")
public Iterable<MyServices> services() {
System.out.println("Inside service without model");
return servicesForOurServices.listAll();
}
}
我的服務模板視圖
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>Services</title>
<!--/*/ <th:block th:include="fragments/headerinc :: head"></th:block> /*/-->
</head>
<body th:fragment="services">
<!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->
<div th:if="${not #lists.isEmpty(services)}" class="container-fluid text-center" id="services">
<h2>SERVICES</h2>
<h4>What we offer</h4>
<div th:each="service : ${services}">
<div class="col-sm-4">
<span th:class="${service.getIcon()}"></span>
<h4 th:text="${service.getName()}"></h4>
<p th:text="${service.getDescription()}"></p>
</div>
</div>
</div>
</body>
</html>
然後我包括片段到我的index.html
<!--/*/ <th:block th:include="services :: #services"></th:block> /*/-->
不過的index.html只呈現h2和h4標記而不是包含服務的div