JSP、Sitemesh 和 Tiles 核心 - 还有其他选择吗?
Tiles 和 Sitemesh 看起来很流行,但与 Ruby (ERB) 或 PHP(Open Power Template)等当前很棒的东西相比,这些东西确实很旧,而且看起来很糟糕。如今,模板引擎允许舒适的模板(插入变量、根据上下文自动转义、迭代可迭代、访问 bean 属性)和布局(例如页眉、页脚以及覆盖和添加到父级中定义的某些部分),无需任何困难的配置,也无需需要更改当前堆栈(例如您的 Web 框架)。
示例parent.html:
<html>
<head>
<title>
<layout:part name="title">
Default title
</layout:part>
</title>
<layout:part name="head" />
</head>
<body>
<div class="menu" layout:part="menu">
default menu
</div>
<div class="content" layout:part="content" />
<div class="footer">
(c) me
</div>
</body>
</html>
示例child.html
<layout:extend file="parent.html">
<layout:fill name="title">
Custom title
</layout:fill>
<layout:fill name="contnet">
the content
{$var} from model
</layout:fill>
</layout:extend>
我正在寻找更好的Facelets,这不需要我更改整个堆栈 - 我不会仅仅为了使用更好的视图而使整个项目适应JSF 或Wicket。
模板引擎不应需要任何额外的 servlet 或过滤器(无基于 URL 的逻辑)。我想以编程方式使用该引擎。一个可能的用例是在 Spring 3 中定义自定义 ViewResolver。
如果没有在配置文件中预先定义布局,那就完美了。如果您只是在模板文件中定义父视图,则不需要这样做。
该框架可能位于 JSP 之上,但并非必须如此。优点是可以使用其他框架(例如 Spring)提供的标签库。
或者也许 Sitemesh/Tiles 中已经存在所有内容,但需要大量配置?如果您知道任何可以实现上述所有目标的示例配置,请告诉我。
相关问题:存在哪些替代方案到 Sitemesh 来帮助在 Spring MVC 应用程序中布局 JSP/JSTL 页面页脚/页眉? - 我的问题也涉及模板,并且不限于 Spring Web MVC。
Tiles and Sitemesh look quite popular but this stuff is really old and look terrible compared to current awesome stuff from e.g. Ruby (ERB) or PHP (Open Power Template). These days template engines allow comfortable templating (inserting variables, autoescaping depending on the context, iterating through Iterables, accessing bean properties) and layouting (e.g. headers, footers and overriding and adding to some parts defined in parent) without any difficult configuration and wihout a need for changing your current stack (e.g. your web framework).
Example parent.html:
<html>
<head>
<title>
<layout:part name="title">
Default title
</layout:part>
</title>
<layout:part name="head" />
</head>
<body>
<div class="menu" layout:part="menu">
default menu
</div>
<div class="content" layout:part="content" />
<div class="footer">
(c) me
</div>
</body>
</html>
Example child.html
<layout:extend file="parent.html">
<layout:fill name="title">
Custom title
</layout:fill>
<layout:fill name="contnet">
the content
{$var} from model
</layout:fill>
</layout:extend>
I'm looking for better Facelets, that won't require me to change the whole stack - I'm not going to adapt the whole project to JSF or Wicket just to use better views.
The template engine should not require any additional servlets or filters (no URL-based logic). I want to use the engine programatically. A possible use case is defining a custom ViewResolver in Spring 3.
It would be perfect if layouts weren't defined up-front in a config file. That's not needed if you just define the parent view in the template file.
The framework may be on top of JSP but doesn't have to. The advantage is a possibility to use taglibs provided by other frameworks (e.g. Spring).
Or maybe everything is already there in Sitemesh/Tiles but needs lots of configuration? If you know of any example configuration that allows to achieve all the mentioned goals, let me know about it.
Related question: what alternatives exist to Sitemesh to help layout JSP/JSTL page footers/headers in a Spring MVC app? - my question refers to templating too, and is not limited to Spring Web MVC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我一直支持JSP 是一种足够好的视图技术,也可用于模板(使用包含)。
对于编程处理,我使用速度,它相当简单明了。
我在 Java 世界中遇到的最好的视图技术是 grails 的 GSP,但是您可能需要将整个 Web 层迁移到 grails,这并不总是一个选择。
最后一点 - 无论你做什么,都不要使用 freemarker。它不必要地复杂,并且您无法轻松完成简单的任务。
I have always supported the idea that JSP is a good-enough view technology that is also usable for templates (using includes)
For programmatic handling I use velocity, which is rather simple and straightforward.
The best view technology I've encountered in the Java world is grails' GSP, but you might need to migrate your whole web layer to grails, which is not always an option.
Just a final note - whatever you do, do not use freemarker. It is unnecessarily complicated and you can't easily achieve simple tasks.