我要重写一个web平台,我正在使用Spring /Spring。该平台的一个主要部分是网站。我正在努力决定使用哪个模板引擎。Thymeleaf似乎是被推荐的,而JSP则不受欢迎。我不知道我的要求是否不寻常,至少对我来说不是这样的:
1)关于Thymeleaf,据我所理解,使用布局将是推荐的(仅?)接近。然而,在我看来,所有的动态内容仍然在每个模板中生成(其中它使用layout:fragment属性流到布局中)。这听起来不太理想,因为这意味着我仍然需要在每个模板中生成布局的动态部分。有没有办法在Thymeleaf布局中包含动态内容,其中的内容(菜单、页脚、twitter-feed等)是与实际内容模板分开生成的?
2) JSP似乎能够很容易地解决这一问题,使用定制的布局标记,其中动态内容使用<jsp:include> -tags,实际内容模板使用<jsp:doBody> -tag。然而,通过阅读Spring文档,我得到的印象是,它被鼓励使用不同的模板引擎JSP。但是,上面描述的方法允许我定义一个动态生成内容的header.jsp、navigation.jsp、footer.jsp和twitterFeed.jsp (基于数据库内容、登录用户等),而实际的内容模板只关注要显示的内容。在Thymeleaf和JSP之间的比较中,我是否遗漏了一些东西,为什么我不选择JSP作为我的项目的模板引擎?
3)按照2)中的方法,我是只限于将所有Java逻辑放在JSP中,用于主布局中包含的模板(页眉、导航、页脚、twitter-feed),还是有更好的方法用类似控制器的类来支持这些存根?
4)是否还有其他与Spring / Spring很好地集成的模板引擎,这比上面提到的任何一个都好?
发布于 2017-11-05 15:56:46
Use可以使用Thymeleaf Ultraq布局创建一个基本模板,它将充当其他模板的装饰器,如下所示:
base-template.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title layout:title-pattern="$CONTENT_TITLE - $LAYOUT_TITLE">Sample</title>
<meta name="description" content=""/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<!-- all CSS links here -->
</head>
<body>
<div class="container">
<div class="content">
<div layout:fragment="page_content">
<!-- Content from other pages which decorate using this template -->
</div>
</div>
</div>
<!-- /.container -->
<!-- All script tags here -->
<th:block layout:fragment="scripts">
<!-- If you have any page specific scripts -->
</th:block>
</body>
</html>然后,其他页面将使用上面的模板作为装饰器,如下所示:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{base-template}">
<head>
<title>This page title</title>
</head>
<div layout:fragment="page_content">
<!-- content for this page -->
</div>
<th:block layout:fragment="scripts">
<!-- add any scripts related to this page -->
</th:block>
</html>语法~{base-template}与Thymeleaf 3继续使用。
您可以继续使用上述方法,而不重复其他页面上的导航、页眉和页脚。
https://stackoverflow.com/questions/47121576
复制相似问题