Java 建议使用模板引擎来减少动态内容的冗余(Spring Boot)
我即将重新编写一个web平台,我正在使用SpringBoot/SpringMVC。该平台的一个主要部分是网站。我正在努力决定使用哪个模板引擎。Thymeleaf似乎是被推荐的,而JSP则不被鼓励。我不确定我的要求是否不同寻常,至少在我看来不是这样的:Java 建议使用模板引擎来减少动态内容的冗余(Spring Boot),java,jsp,spring-mvc,spring-boot,thymeleaf,Java,Jsp,Spring Mvc,Spring Boot,Thymeleaf,我即将重新编写一个web平台,我正在使用SpringBoot/SpringMVC。该平台的一个主要部分是网站。我正在努力决定使用哪个模板引擎。Thymeleaf似乎是被推荐的,而JSP则不被鼓励。我不确定我的要求是否不同寻常,至少在我看来不是这样的: 我不想在不同的模板中重复我自己,它们都应该显示在“主模板/布局”中 主模板/布局将具有导航和页脚,它们具有动态内容(例如,它不仅是动态的主要内容) 1) 根据我的理解,使用Thymeleaf,使用布局将是推荐的(唯一?)方法。然而,在我看来,所
- 我不想在不同的模板中重复我自己,它们都应该显示在“主模板/布局”中
- 主模板/布局将具有导航和页脚,它们具有动态内容(例如,它不仅是动态的主要内容)
layout:fragment
属性流入布局)。这听起来不太理想,因为这意味着我仍然需要在每个模板中生成布局的动态部分。如果内容(菜单、页脚、twitter提要等)是与实际内容模板分开生成的,那么在Thymeleaf布局中是否无法包含动态内容
2) JSP似乎能够相当容易地解决这个问题,使用一个用于布局的自定义标记,该标记具有用于动态内容的
-标记和用于实际内容模板的
-标记。然而,通过阅读Spring引导文档,我不知何故得到了这样的印象,即鼓励使用与JSP不同的模板引擎。然而,上述方法允许我定义一个动态生成内容(基于数据库内容、登录用户等)的header.jsp
、navigation.jsp
、footer.jsp
和twitterFeed.jsp
,而实际的内容模板只关注要显示的内容。在这里,我在比较Thymeleaf和JSP时是否遗漏了一些东西,为什么我不选择JSP作为我的项目的模板引擎呢
3) 如果采用第2)节中所述的方法,我将仅限于将所有Java逻辑放入主布局(页眉、导航、页脚、twitter提要)中包含的模板的JSP中,还是有更好的方法使用类似控制器的类来备份这些存根
4) 是否有任何其他模板引擎能够与Spring MVC/Spring Boot很好地集成,这将是上述任何一个引擎的更好选择?可以使用它创建一个基础模板,作为其他模板的装饰器,如下所示:
<!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.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一起使用
您可以继续使用上述方法,而不必在其他页面上重复导航、页眉和页脚。使用可以创建一个基础模板,该模板将作为其他模板的装饰器,如下所示:
<!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.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一起使用
您可以继续使用上述方法,而不必在其他页面上重复导航、页眉和页脚。不要使用JSP。它不是一个模板引擎。这是糟糕时期的可怕宿醉。JSP被编译成Java代码,并作为一个单独的servlet进行部署——然后将请求转发给它。老实说,我完全无法理解您的担忧,也无法理解您所说的“每个模板中仍会生成很多动态内容”的含义。我不确定你是否混淆了服务器端模板和AJAX之类的东西,但这两种方法远远不是等价的。我对Thymeleaf方法的担心是,在我看来,我必须在每个子模板中定义父模板应该如何呈现菜单、推特提要、,页脚等等——不管子模板是home.html、login.html、content.html、blogpost.html等等。我不是JSP的拥护者,我知道它的历史和缺点(甚至Spring Boot也不鼓励使用它)。我只需要确信Thymeleaf(或任何其他)可以完成这项工作。不要使用JSP。它不是一个模板引擎。这是糟糕时期的可怕宿醉。JSP被编译成Java代码,并作为一个单独的servlet进行部署——然后将请求转发给它。老实说,我完全无法理解您的担忧,也无法理解您所说的“每个模板中仍会生成很多动态内容”的含义。我不确定你是否混淆了服务器端模板和AJAX之类的东西,但这两种方法远远不是等价的。我对Thymeleaf方法的担心是,在我看来,我必须在每个子模板中定义父模板应该如何呈现菜单、推特提要、,页脚等等——不管子模板是home.html、login.html、content.html、blogpost.html等等。我不是JSP的拥护者,我知道它的历史和缺点(甚至Spring Boot也不鼓励使用它)。我只需要确信Thymeleaf(或任何其他)可以完成这项工作。这是我一直在研究的例子,从中我得出结论,使用这种方法,我还必须在所有模板中定义所有动态内容(我知道当导航、页眉、页脚等是静态的时,它工作得很好)(而base-template.html仅确定此内容站点在静态整体模板中的位置)