java thymeleaf 是一种什么样的解决方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38806245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What kind of a solution is thymeleaf?
提问by Niklas
I read that thymeleaf is preferred instead of JSP nowadays. Which problem does thymeleaf solve that JSP can't? How is thymeleaf compared to other templating engines?
我读到 thymeleaf 现在比 JSP 更受欢迎。thymeleaf 解决了哪些 JSP 不能解决的问题?thymeleaf 与其他模板引擎相比如何?
回答by Milo? Milivojevi?
Well, this is really an opinion-based question and such will be my answer :)
好吧,这确实是一个基于意见的问题,这就是我的答案:)
The main difference is that JSPs are compiled to Java servlet classes whereas Thymeleaf reads the template file, parses its DOM and then applies the model to it. This introduces a performance overhead and gives JSPs a performance edge, even with Thymeleaf's template caching enabled. On the other hand, it makes Thymeleaf much easier to develop with and here's why:
主要区别在于 JSP 被编译为 Java servlet 类,而 Thymeleaf 读取模板文件,解析其 DOM,然后将模型应用于它。这引入了性能开销并为 JSP 提供了性能优势,即使启用了 Thymeleaf 的模板缓存。另一方面,它使 Thymeleaf 更易于开发,原因如下:
Thymeleaf uses plain HTML as its templates. You can either choose a pure HTML5 approach with
data-th
attributes or the original custom-namespaceth:
attributes. The benefit of using plain HTML is that you can open it in a browser, which makes it easier for the designers to do their work: both the designers and developers can work on the same file.Thymeleaf templates can be used without an application server and they also work nicely with embedded servers. You don't have to jump through hoops to do integration testing of your templates, which you can do either through Thymeleaf's own standalone template testing libraryor as a part of functional tests, e.g. integration tests of your Spring MVC controllers.
Thymeleaf has more lightweight and more powerful syntax and better integration with Spring ecosystem. It was designed to integrate with Spring from its conception, but you can also use it outside of Spring. I found it useful for email and report generation.
Thymeleaf templates allow for great reuse; you can create small template fragments and reuse them in other templates, which is a lifesaver in projects with lots of templates. It also has great layout support out of the box although you can also find community pluginsif you need more flexibility/power.
Thymeleaf 使用纯 HTML 作为其模板。您可以选择带有
data-th
属性的纯 HTML5 方法或原始的自定义命名空间th:
属性。使用纯 HTML 的好处是您可以在浏览器中打开它,这使设计人员可以更轻松地完成他们的工作:设计人员和开发人员可以处理同一个文件。Thymeleaf 模板可以在没有应用程序服务器的情况下使用,并且它们也可以很好地与嵌入式服务器配合使用。您不必费力地对模板进行集成测试,您可以通过Thymeleaf 自己的独立模板测试库或作为功能测试的一部分进行,例如 Spring MVC 控制器的集成测试。
Thymeleaf 具有更轻量级和更强大的语法以及与 Spring 生态系统更好的集成。它的设计初衷是为了与 Spring 集成,但您也可以在 Spring 之外使用它。我发现它对电子邮件和报告生成很有用。
Thymeleaf 模板允许大量重复使用;您可以创建小的模板片段并在其他模板中重用它们,这在具有大量模板的项目中是一个救星。它也有很好的开箱即用的布局支持,尽管如果您需要更多的灵活性/功能,您也可以找到社区插件。
Disclaimer: I haven't done any Thymeleaf development in over a year but from what I can see, it hasn't changed that much. You can also take a look at this articlefrom Spring - while it is a bit old, it does illustrate the main differences.
免责声明:我已经一年多没有做任何 Thymeleaf 开发了,但从我所见,它并没有太大变化。您还可以从 Spring 中查看这篇文章- 虽然它有点旧,但它确实说明了主要区别。