具有主模板和子视图的 Java HTML 视图引擎

发布于 2024-12-23 14:43:52 字数 1227 浏览 3 评论 0原文

我正在拼命寻找一个能够满足三个主要要求的 Java HTML 视图引擎:

  1. 支持主​​模板页面。
  2. HTML 模板可以用作其他模板中的子视图。
  3. HTML 模板不必由配置文件或 Java 类支持。

它适用于Java Web应用程序,主要由只读页面和一些带有表单的页面组成。我很可能会将它与 MVC 框架结合使用。

主模板页面

HTML 的主要结构应由 HTML 主页面定义。不同的页面仅提供放入母版页以创建最终页面的核心内容。这不仅仅是包含页眉和页脚。

子视图

页面应该能够使用其他 HTML 页面/模板作为其自身内容中的子视图。它应该能够传递至少一个参数来提供子视图需要显示的数据。此外,应该可以在子视图中递归地使用更多子视图。同样,这超出了简单的包含机制。

无支持

HTML 模板应由单个文件组成,该文件基本上是 HTML 或 XML 页面,其中某些部分将根据提供的数据进行替换。他们不需要任何额外的(每个模板)配置文件。而且他们不应该需要实现任何 Java 类来进行支持。

我已经看过很多 Java 模板引擎。但他们似乎都不符合这些要求。 (在 .NET 世界中,带有 Razor 视图引擎的 ASP.NET MVC 将是完美的选择。)

更新:

到目前为止,我已经研究了以下引擎(如果有,请告诉我)我忽略了一种使用这些引擎之一来实现我的要求的方法):

  • JSP:只有一个非常基本的包含机制,没有主模板或适当的子视图范围等。
  • 速度 >:稍微高级一点包含机制,但没有母版页。
  • FreeMarker:包括机制,无母版页。
  • Tapestry:良好的基于​​组件的子视图以及模板机制。然而,在没有框架其余部分的情况下使用模板引擎部分似乎不太可能,因为模板引擎部分过于以页面为中心而无法与 MVC 框架相结合。
  • Tiles:每个最终页面需要两个 jsp 页面。两个层(tiles 和 jsp)使得它过于复杂,特别是对于子视图。

更新(2): 我更改了一些术语:视图引擎而不是模板引擎子视图而不是组件

I'm desperately looking for a Java HTML view engine that fulfills three main requirements:

  1. Support for master template pages.
  2. HTML templates can be used as subviews within other templates.
  3. HTML templates do not have to be backed by config files or Java classes.

It's for a Java web application that mainly consists of read-only page and a few pages with forms. Most likely I'll use it in combination with an MVC framework.

Master Template Pages

The main structure of the HTML should be defined by a master HTML page. The different pages just provide the core content that's put into the master page to create the final page. This is more than just the inclusion of header and footer.

Subviews

A page should be able to use other HTML pages/templates as subviews within its own content. It should be able to pass at least one parameter to provide the data that the subview needs to display. Furthermore, it should be possible to recursively use further subviews within a subview. Again, this goes beyond a simple include mechanism.

No Backing

HTML templates should consist of a single file that basically is an HTML or XML page where certain parts will be substituted based on the provided data. They shouldn't need any additional (per template) config files. And they shouldn't have the need to implement any Java classes for backing.

I've already had a look at many Java template engine. But neither of them seems to meet these requirements. (In the .NET world, ASP.NET MVC with the Razor view engine would be a perfect fit though.)

Update:

So far, I've looked at the following engines (please let me know if I've overlooked a way to achieve my requirements with one of these engines):

  • JSP: Has just a very basic inclusion mechanism without master templates or proper scoping for subviews etc.
  • Velocity: Has a slightly advanced inclusion mechanism, but no master pages.
  • FreeMarker: Include mechanism, no master pages.
  • Tapestry: Good component based subviews as well as a templating mechanism. However, it doesn't seem to be easily possible to use template engine part without the rest of the framework, which is too page-centric to be combined with an MVC framework.
  • Tiles: Requires two jsp pages per final page. The two layers (tiles and jsp) makes it too complex, in particular for subviews.

Update (2):
I've changed some term: view engine instead of template engine, subviews instead of components.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离去的眼神 2024-12-30 14:43:52

您还没有真正看过模板引擎。 JSP、Velocity 和 FreeMarker 不是模板引擎(按照您的意思是模板引擎)。它们是允许动态生成标记并获取数据以从 Java 对象生成的 HTML 中显示的语言。 Tapestry 是一个完整的基于组件的 Web 应用程序框架。

如果您使用 JSP 生成 HTML 页面,则可以在 JSP 之上使用模板引擎(如 Tiles 或 SIteMesh),它将处理模板,从而允许整页的每个“组件”有一个 JSP。如果没有像 Stripes、Spring MVC 或 Struts2 这样的 Web MVC 框架,通常不应使用 JSP。所有这些都有自己的模板支持,和/或支持集成其他模板(例如 SiteMesh 或 Tiles)。

You haven't really looked at template engines. JSP, Velocity and FreeMarker are not template engines (with your meaning of a template engine). They're languages allowing to generate markup dynamically, and get the data to display in the generated HTML from Java objects. Tapestry is a complete web application framework, based on components.

If you're using JSP to generate the HTML pages, you can use a template engine on top of JSP like Tiles or SIteMesh, which will handle the templating, and thus allow to have one JSP per "component" of a full page. JSP should generaly not be used without an web MVC framework like Stripes, Spring MVC or Struts2. All of these either have their own templating support, and/or support integrating another one like SiteMesh or Tiles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文