使用百里香叶代替jsp作为春天有什么好处(What is the advantage of using Thymeleaf instead of JSP in Spring?)

编程入门 行业动态 更新时间:2024-10-14 04:28:42
使用百里香叶代替jsp作为春天有什么好处(What is the advantage of using Thymeleaf instead of JSP in Spring?)

我想知道在春天使用Thymeleaf代替jsp可以获得什么样的优势。

I'm wondering what are the advantages I can get by using Thymeleaf instead of JSP for the view in Spring.

最满意答案

通常引用的优点是:

Spring集成是Thymeleaf的第一类方面( 这里有大量文档 )。 在这种情况下,“头等舱”意味着它不是偶然的或部分实施的,它是Thymeleaf的一个深思熟虑,受到良好支持的方面。 Spring表达式语言比JSP表达式语言更强大。 “更强大”听起来是主观的,但在这种情况下,我们讨论的是与Spring的集成,因此Spring自己的EL(具有模型属性,形式支持bean和国际化的意识)提供了比vanilla JSP表达式更多的开箱即用。 Thymeleaf提供了有用的格式化实用程序,例如${#calendars.format(...)} , ${#strings.capitalize(...)} ,它们与Spring很好地集成,例如,您可以将Spring MVC传播的模型bean传递给这些功能。 Thymeleaf缩短了构建/部署/测试反馈循环。 这是一个例子; 假设您要更改网页的布局或样式。 在Thymeleaf中,这涉及:(1)打开.html模板,编辑它并(可能)编辑它的链接.css文件; (2)在浏览器中按F5刷新; (3)重复直到开心。 相比之下,JSP实现中的相同活动将涉及:(1)将应用程序部署到开发服务器中; (2)启动它; (3)做出一些改变; (4)重新部署(或热部署)变更; (5)重复直到开心。 最后一点强烈暗示了JSP的这种局限性; 它们不能在容器外使用。 相比之下,Thymeleaf模板可以在容器外部使用。 例如; Spring的MVC测试支持与Thymeleaf完美集成,可以允许您在测试环境中渲染(并测试/断言)已解析的Thymeleaf模板,而无需启动应用程序。 Thymeleaf模板看起来像HTML(术语是“自然模板”),它们甚至可以作为静态内容(带有模板/原型数据)在浏览器中呈现,因此如果您的Web层和后端由不同的人/专业化创作,那么Thymeleaf是Web设计人员比JSP更容易处理。

The commonly cited advantages are:

Spring integration is a first class aspect of Thymeleaf (plenty of documentation here). "First class", in this context, means that it is not accidental or partially implemented, it is a deliberate, well supported aspect of Thymeleaf. The Spring Expression Language is more powerful than JSP Expression Language. "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. Thymeleaf provides useful formatting utilities such as ${#calendars.format(...)}, ${#strings.capitalize(...)} which are well integrated with Spring e.g. you can pass model beans propagated by Spring MVC into these functions. The build/deploy/test feedback loop is shortened by 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; (2) hit F5 to refresh in the browser; (3) repeat until happy. By contrast, the same activity in a JSP implementation would involve: (1) deploy the application into a development server; (2) start it up; (3) make some changes; (4) redeploy (or hot deploy) the changes; (5) repeat until happy. The last point hints strongly at this limitation of JSPs; they cannot be used outside of a container. By contrast, Thymeleaf templates can be used outside of a container. 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. 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,using,代替,jsp,电脑培训,计算机培训,IT培训"/> <meta name="descri

本文发布于:2023-04-29 03:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334482.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:百里   有什么好处   春天   jsp   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!