简体   繁体   English

在Spring中使用Thymeleaf代替JSP有什么好处?

[英]What is the advantage of using Thymeleaf instead of JSP in Spring?

我想知道在Spring中使用Thymeleaf而不是JSP来获得什么样的优势。

The commonly cited advantages are: 通常引用的优点是:

  • Spring integration is a first class aspect of Thymeleaf (plenty of documentation here ). Spring集成是Thymeleaf的第一类方面( 这里有大量文档 )。 "First class", in this context, means that it is not accidental or partially implemented, it is a deliberate, well supported aspect of Thymeleaf. 在这种情况下,“头等舱”意味着它不是偶然的或部分实施的,它是Thymeleaf的一个深思熟虑,受到良好支持的方面。
  • The Spring Expression Language is more powerful than JSP Expression Language. Spring表达式语言比JSP表达式语言更强大。 "More powerful" sounds subjective but in this case we are talking about integration with Spring so Spring's own EL (with its awareness of model attributes, form backing bean and internationalization) offers more out-of-the-box than vanilla JSP expressions. “更强大”听起来是主观的,但在这种情况下,我们讨论的是与Spring的集成,因此Spring自己的EL(具有模型属性,形式支持bean和国际化的意识)提供了比vanilla JSP表达式更多的开箱即用。
  • Thymeleaf provides useful formatting utilities such as ${#calendars.format(...)} , ${#strings.capitalize(...)} which are well integrated with Spring eg you can pass model beans propagated by Spring MVC into these functions. Thymeleaf提供了有用的格式化实用程序,例如${#calendars.format(...)}${#strings.capitalize(...)} ,它们与Spring很好地集成,例如,您可以将Spring MVC传播的模型bean传递给这些职能。
  • The build/deploy/test feedback loop is shortened by Thymeleaf. Thymeleaf缩短了构建/部署/测试反馈循环。 Here's an example; 这是一个例子; let's say you want to change the layout or style for a webpage. 假设您要更改网页的布局或样式。 In Thymeleaf this involves: (1) open the .html template, edit it and (possibly) edit it's linked .css file; 在Thymeleaf中,这涉及:(1)打开.html模板,编辑它并(可能)编辑它的链接.css文件; (2) hit F5 to refresh in the browser; (2)在浏览器中按F5刷新; (3) repeat until happy. (3)重复直到开心。 By contrast, the same activity in a JSP implementation would involve: (1) deploy the application into a development server; 相比之下,JSP实现中的相同活动将涉及:(1)将应用程序部署到开发服务器中; (2) start it up; (2)启动它; (3) make some changes; (3)做出一些改变; (4) redeploy (or hot deploy) the changes; (4)重新部署(或热部署)变更; (5) repeat until happy. (5)重复直到开心。
  • The last point hints strongly at this limitation of JSPs; 最后一点强烈暗示了JSP的这种限制; they cannot be used outside of a container. 它们不能在容器外使用。 By contrast, Thymeleaf templates can be used outside of a container. 相比之下,Thymeleaf模板可以在容器外部使用。 For example; 例如; Spring's MVC test support is well integrated with Thymeleaf and can allow you to render (and test/assert against) a resolved Thymeleaf template within a test context without starting your application. Spring的MVC测试支持与Thymeleaf完美集成,可以允许您在测试环境中渲染(并测试/断言)已解析的Thymeleaf模板,而无需启动应用程序。
  • Thymeleaf templates look like HTML (the term is "natural template"), they can even be rendered in the browser as static content (with template/prototype data) so if your web layer and backend are authored by different people/specialisations then Thymeleaf is easier for a web designer to deal with than JSP. Thymeleaf模板看起来像HTML(术语是“自然模板”),它们甚至可以在浏览器中呈现为静态内容(带有模板/原型数据),因此如果您的Web层和后端由不同的人/专业化创作,那么Thymeleaf是Web设计人员比JSP更容易处理。

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

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