推荐的模板引擎,以减少动态内容的冗余(Spring Boot) [英] Recommended template engine to reduce redundancy of dynamic content (Spring Boot)

查看:73
本文介绍了推荐的模板引擎,以减少动态内容的冗余(Spring Boot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将要重写一个Web平台,并且我正在使用Spring Boot/Spring MVC.该平台的主要部分是网站.我正在努力决定要使用哪个模板引擎.似乎建议使用Thymeleaf,但不建议使用JSP.我不确定我的要求是否异常,至少对我来说听起来不像这样:

I am about to re-write a web-platform, and I am using 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. I am not sure if my requirements are unusual, at least they do not sound like that to me:

  • 我不想在不同的模板中重复我自己,它们都应该显示在主模板/布局"中
  • 主模板/布局将具有导航和页脚,它们具有动态内容(例如,不仅主要内容是动态的)

1)从Thymeleaf的角度,据我所知,推荐使用Layouts(仅?)方法.但是,在我看来,所有动态内容仍然在每个模板中生成(使用layout:fragment属性流入布局中).这听起来不理想,因为这意味着我仍然必须在每个模板中生成布局的动态部分.没有办法在Thymeleaf布局中包含动态内容,而其中的内容(菜单,页脚,twitter-feed等)是与实际内容模板分开生成的?

1) With Thymeleaf, from what I have been able to understand, using Layouts would be the recommended (only?) approach. 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). 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?

2)JSP似乎可以使用布局的自定义标签轻松解决此问题,该标签具有用于动态内容的<jsp:include> -tags和用于实际内容模板的<jsp:doBody> -tag.但是,通过阅读Spring Boot文档,我以某种方式得到的印象是,鼓励使用与JSP不同的模板引擎.但是,上述方法将让我定义一个header.jspnavigation.jspfooter.jsptwitterFeed.jsp,它们动态地生成内容(基于数据库内容,登录的用户等),而实际的内容模板纯粹专注于在要显示的内容上.在Thymeleaf和JSP之间进行比较时,我缺少什么了,为什么我不选择JSP作为项目的模板引擎?

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. However, from reading the Spring Boot documentation, I somehow got the impression that it is encouraged to use a different template-engine that 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. 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?

3)使用2)中所述的方法,我是否仅限于将我的所有Java逻辑放入主布局(页眉,导航,页脚,Twitter提要)中包含的模板的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?

4)是否还有其他与Spring MVC/Spring Boot很好地集成的模板引擎,这将是上述任何模板引擎的更好选择?

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?

推荐答案

使用可以使用 Thymeleaf Ultraq布局,以创建一个基本模板,该模板将充当其他模板的装饰器,如下所示:

Use can use Thymeleaf Ultraq Layout to create a base template which will act as a decorator for your other templates as shown below:

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>

~{base-template}语法从Thymeleaf 3开始使用.

The syntax ~{base-template} is used with Thymeleaf 3 onward.

您可以继续上述方法,而不必在其他页面上重复导航,页眉和页脚.

You can proceed with the above approach and not repeat the navigations, headers and footers on your other pages.

这篇关于推荐的模板引擎,以减少动态内容的冗余(Spring Boot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆