Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 使用Thymeleaf和Spring MVC构建大视图_Spring Mvc_Thymeleaf - Fatal编程技术网

Spring mvc 使用Thymeleaf和Spring MVC构建大视图

Spring mvc 使用Thymeleaf和Spring MVC构建大视图,spring-mvc,thymeleaf,Spring Mvc,Thymeleaf,我将使用SpringMVC、Thymeleaf和飞碟,用java生成一份pdf报告(大约20张幻灯片长)。我希望能够根据不同的幻灯片构造代码,这样我就可以轻松地添加和删除幻灯片,而不是将所有幻灯片的所有代码都放在一块中。最后,在SpringMVC和Thymeleaf完成之后,我想我将有很多XHTML和CSS准备好发送到飞碟上生成PDF 我对Spring MVC没有做太多的工作,但我的感觉是,首先要做控制器的工作,例如获取数据,处理数据,然后将必要的数据放在模型上,这样Thymeleaf可以继续并

我将使用SpringMVC、Thymeleaf和飞碟,用java生成一份pdf报告(大约20张幻灯片长)。我希望能够根据不同的幻灯片构造代码,这样我就可以轻松地添加和删除幻灯片,而不是将所有幻灯片的所有代码都放在一块中。最后,在SpringMVC和Thymeleaf完成之后,我想我将有很多XHTML和CSS准备好发送到飞碟上生成PDF

我对Spring MVC没有做太多的工作,但我的感觉是,首先要做控制器的工作,例如获取数据,处理数据,然后将必要的数据放在模型上,这样Thymeleaf可以继续并基于模板和模型上的数据渲染视图


如何以良好的模块化方式划分java和Thymeleaf中的代码部分?任何人都可以从一个好的设计中获得灵感,或者可以在web上的某个地方向我指出,我可以在那里找到关于这方面的好信息?

在您的案例中,我建议将Thymeleaf模板分为三个部分-主模板、幻灯片模板和内容模板。您可以从SpringMVC控制器动态填充报告内容,也可以仅在Thymeleaf中以静态方式构建报告内容

我提供了我认为最好的框架模板结构(所有模板都应该位于根模板路径上)。若要添加幻灯片,只需创建包含内容的新模板,并在主模板中插入新行即可。如果要删除幻灯片,只需删除相应的行即可

主模板-index.html


幻灯片模板-Slide.html


内容模板1-content1.html


  • 内容1
内容模板2-content2.html


内容2
内容模板3-content3.html


内容3


如果您作为同一页面的一部分进行显示,您应该能够创建片段化的ELEAF视图,并将其导入到一个“主”视图中。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
    <body>
        <th:block th:include="slide :: slide" th:with="content=${'content1'}"></th:block>
        <th:block th:include="slide :: slide" th:with="content=${'content2'}"></th:block>
        <th:block th:remove="tag" th:include="slide :: slide" th:with="content=${'content3'}"></th:block>
    </body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
    <body th:fragment="slide">
        <div class="slide">
            <div th:replace="${content} :: content"></div>
        </div>
    </body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
    <body>
        <ul th:fragment="content">
            <li>Content 1</li>
        </ul>
    </body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
    <body>
        <span th:fragment="content">Content 2</span>
    </body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
    <body>
       <p th:fragment="content">
           Content 3
       </p>
    </body>
</html>