UseThymeleaf模板视图,将数据从数据库检索到另一个模板视图

时间:2017-02-06 11:09:48

标签: spring-mvc thymeleaf

我正在尝试将已经从我的数据库检索数据的视图服务包含到我的index.html页面中。我正在使用Spring和Thymeleaf。

这是我的服务控制器

    @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

But the index.html only render the h2 and h4 tag and not the div containing the services 如果我访问services.html,一切都很好   and if I access the services.html everything renders fine 提前致谢

1 个答案:

答案 0 :(得分:0)

您是否检查过浏览器中的元素以查看是否有任何错误提取图片?渲染后怀疑URL不正确。