简体   繁体   English

推荐的模板引擎,以减少动态内容的冗余(Spring Boot)

[英]Recommended template engine to reduce redundancy of dynamic content (Spring Boot)

I am about to re-write a web-platform, and I am using Spring Boot/Spring MVC. 我要重新编写一个Web平台,并且正在使用Spring Boot / Spring MVC。 A major part of the platform is the website. 该平台的主要部分是网站。 I am struggling deciding which template-engine to use. 我正在努力决定要使用哪个模板引擎。 Thymeleaf seems to be recommended, while JSP discouraged. 推荐使用Thymeleaf,但不建议使用JSP。 I am not sure if my requirements are unusual, at least they do not sound like that to me: 我不确定我的要求是否异常,至少在我看来这些要求听起来不像是:

  • I do not want to repeat myself in the different templates, they should all be displayed inside a "master template/layout" 我不想在不同的模板中重复说明,它们都应显示在“主模板/布局”中
  • The master template/layout will have navigation and footer, which have dynamic content (eg. it is not only the main content that is dynamic) 主模板/布局将具有导航和页脚,它们具有动态内容(例如,不仅主要内容是动态的)

1) With Thymeleaf, from what I have been able to understand, using Layouts would be the recommended (only?) approach. 1)从Thymeleaf的角度出发,据我所知,推荐使用Layouts(仅?)方法。 However, it looks to me like all dynamic content much still be generated in each template (where it flows into the layout using the layout:fragment attribute). 但是,在我看来,所有动态内容仍然会在每个模板中生成(它使用layout:fragment属性流入布局)。 This sounds less than ideal, as it would mean I would still have to generate the dynamic part of the layout in each template. 这听起来不太理想,因为这意味着我仍然必须在每个模板中生成布局的动态部分。 Is there no way to include dynamic content in Thymeleaf layouts, where the content (menu, footer, twitter-feed etc) is generated separately from the actual content-template? 没有办法在Thymeleaf布局中包含动态内容,其中内容(菜单,页脚,twitter-feed等)是与实际内容模板分开生成的吗?

2) JSP seems to be able to solve this rather easily, using a custom tag for the layout, that has <jsp:include> -tags for the dynamic content and a <jsp:doBody> -tag for the actual content-template. 2)JSP似乎可以使用布局的自定义标签轻松解决此问题,该标签具有用于动态内容的<jsp:include> -tags和用于实际内容模板的<jsp:doBody> -tag。 However, from reading the Spring Boot documentation, I somehow got the impression that it is encouraged to use a different template-engine that JSP. 但是,通过阅读Spring Boot文档,我以某种方式得到的印象是,鼓励使用与JSP不同的模板引擎。 The approach described above would however let me define a header.jsp , navigation.jsp , footer.jsp and twitterFeed.jsp that generates the content dynamically (based on database content, logged in user etc), while the actual content-template purely focuses on the content to display. 但是,上述方法让我定义一个header.jspnavigation.jspfooter.jsptwitterFeed.jsp ,它们动态地生成内容(基于数据库内容,登录的用户等),而实际的内容模板纯粹是关注的在要显示的内容上。 Is there something I am missing in my comparison between Thymeleaf and JSP here, why would I not chose JSP as my project's template engine? 在Thymeleaf和JSP之间进行比较时,我缺少什么,为什么我不选择JSP作为项目的模板引擎?

3) With the approach meantioned in 2), would I be limited to putting all my Java logic in the JSPs for the templates included in the main layout (header, navigation, footer, twitter-feed), or is there a better way to back these stubs up with a controller-like class? 3)使用2)中所述的方法,我是否仅限于将我的所有Java逻辑放入主布局中的模板(页眉,导航,页脚,Twitter提要)的JSP中,还是有更好的方法用控制器类支持这些存根?

4) Are there any other template engines that integrate well with Spring MVC / Spring Boot, that would be a better choice that any of the above mentioned ones? 4)是否有其他模板引擎可以很好地与Spring MVC / Spring Boot集成在一起,这将是上述任何一种更好的选择?

Use can use Thymeleaf Ultraq Layout to create a base template which will act as a decorator for your other templates as shown below: Use可以使用Thymeleaf Ultraq Layout创建一个基本模板,该模板将充当其他模板的装饰器,如下所示:

base-template.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>

Then the other pages will use the above template as a decorator as shown below: 然后其他页面将使用上面的模板作为装饰器,如下所示:

<!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>

The syntax ~{base-template} is used with Thymeleaf 3 onward. 语法~{base-template}与Thymeleaf 3及更高版本一起使用。

You can proceed with the above approach and not repeat the navigations, headers and footers on your other pages. 您可以继续上述方法,而不必在其他页面上重复导航,页眉和页脚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM