Java 为新的 Spring MVC 项目在 Thymeleaf 和 Angular 之间进行选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44301334/
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
Choosing between Thymeleaf and Angular for a new Spring MVC project
提问by davioooh
I'm starting a new Spring Bootweb application and I need to make a choice about how to implement front-end.
我正在启动一个新的Spring BootWeb 应用程序,我需要选择如何实现前端。
I'm really comfortable with Thymeleaftemplating framework that I used in several projects, but I'm evaluating Angular (v2)as a possible alternative.
我对在多个项目中使用的Thymeleaf模板框架非常满意,但我正在评估Angular (v2)作为可能的替代方案。
I'm quite new to client-side MVCframeworks and I'd like to understand if there are some guidelines to decide which option is the best for a web project.
我对客户端MVC框架很陌生,我想了解是否有一些指导方针来决定哪个选项最适合 Web 项目。
I've read a lot of posts and tutorials about this, but they all seem to describe personal opinions or to be just coding experiments...
我已经阅读了很多关于此的帖子和教程,但它们似乎都描述了个人观点或只是编码实验......
Are there some application requirements that objectivelysuggest to use a client-side approach instead of a server-side implementation?
Is it possible and effective to use both the technologies together?
是否有一些应用程序要求客观地建议使用客户端方法而不是服务器端实现?
将这两种技术结合使用是否可能且有效?
回答by Snickers3192
The choice isn't whether to use one JavaScript library or Thymeleaf, that would be an artificial constraint you created, they are both different animals. Thymeleaf is for templating, JQuery is a client side library for making dynamic pages. It would make more sense to choose between templating libraries or choosing between JavaScript libraries, but both can go nicely together.
选择不是使用一个 JavaScript 库还是 Thymeleaf,这将是您创建的人为约束,它们都是不同的动物。Thymeleaf 用于模板,JQuery 是用于制作动态页面的客户端库。在模板库或 JavaScript 库之间进行选择会更有意义,但两者可以很好地结合在一起。
In general, your templating should not affect your JavaScript code, some people do template their Javascript code, but IMHO this is a terrible idea. Instead use Templating to replace tag attributes in HTML, and Javascript for making your website dynamic with whatever library you choose.
一般来说,您的模板不应该影响您的 JavaScript 代码,有些人确实对他们的 Javascript 代码进行了模板化,但恕我直言,这是一个糟糕的主意。而是使用模板来替换 HTML 中的标签属性,以及使用您选择的任何库来使您的网站动态化的 Javascript。
I'm not sure what you mean by
我不确定你的意思
client-side approah instead of a server-side implementation
客户端方法而不是服务器端实现
But what I see nowadays are 2 different approaches, creating Microservies VS Monolith web application. If you develop your website entirely in Thymeleaf you will not make it very reusable. Netflix, Amazon, Uber etc. all create services (sometimes called micro-services) which are reused throughout the organisation via a REST API.
但我现在看到的是 2 种不同的方法,创建 Microservies VS Monolith Web 应用程序。如果您完全在 Thymeleaf 中开发您的网站,您将无法使其具有很高的可重用性。Netflix、Amazon、Uber 等都创建了服务(有时称为微服务),这些服务通过 REST API 在整个组织中重复使用。
If you only have a website implemented in Thymeleaf it will not be possible for another application (think mobile) to access any services created by your application, whereas if you create a REST API for your app then you can keep reusing your server back-end for when you need to make a mobile app, or another web app with similar requirements.
如果您只在 Thymeleaf 中实现了一个网站,则其他应用程序(想想移动设备)将无法访问您的应用程序创建的任何服务,而如果您为您的应用程序创建了 REST API,那么您可以继续重用您的服务器后端当您需要制作移动应用程序或具有类似要求的其他网络应用程序时。
Of course if your services are not going to be re-used this might not matter to you and you might be very proficient in doing things a certain way albeit not being the most reusable, of course we are not always creating the next Amazon. But I still believe it is good idea to follow best practices, even if its just to get better at doing things a certain way.
当然,如果您的服务不会被重复使用,这对您来说可能无关紧要,您可能非常擅长以某种方式做事,尽管不是最可重复使用的,当然我们并不总是在创建下一个亚马逊。但我仍然相信遵循最佳实践是个好主意,即使只是为了更好地以某种方式做事。
回答by Erik Pearson
Thymeleaf and AngularJS are not mutually exclusive. You can certainly use them together, depending on what you are trying to accomplish. If you are all in on writing a single-page dynamic web application, Thymeleaf is probably unnecessary.
Thymeleaf 和 AngularJS 并不相互排斥。您当然可以将它们一起使用,具体取决于您要完成的任务。如果您致力于编写单页动态 Web 应用程序,Thymeleaf 可能是不必要的。
Speaking to your question about client side frameworks vs. server-side ones, I'm an Enterprise Software Engineer, so I am not creating commercial software and my priority is less about how pretty it looks (a little Bootstrap does the trick) and more about stability, browser compatibility (even with older browsers), and maintainability. I personally avoid the single page, dynamic web applications because I find the code base more difficult to manage in non-trivial applications; a large Javascript code base can be a bear, in my opinion. Building up my pages primarily on the server side provides me with better debugging capabilities (Java is going to give you a lot of compile time help that you don't get with Javascript) and easier logging. I do use javascript (mostly just jquery) on the client side, but generally my web apps are built to degrade gracefully if the user has javascript turned off. Again, these are internal, utilitarian, applications to support the business. I don't have time to write a whole thesis on the subject and there are plenty of nuances that I'm not covering, but hopefully this is useful.
谈到你关于客户端框架与服务器端框架的问题,我是一名企业软件工程师,所以我不是在创建商业软件,我的首要任务不是它看起来有多漂亮(一点 Bootstrap 就行了)等等关于稳定性、浏览器兼容性(即使是旧浏览器)和可维护性。我个人避免使用单页、动态 Web 应用程序,因为我发现在非平凡的应用程序中代码库更难以管理;在我看来,大型 Javascript 代码库可能是一只熊。主要在服务器端构建我的页面为我提供了更好的调试功能(Java 将为您提供很多使用 Javascript 无法获得的编译时间帮助)和更容易的日志记录。我确实在客户端使用了 javascript(主要是 jquery),但通常我的网络应用程序会在用户关闭 javascript 时优雅地降级。同样,这些是支持业务的内部、实用的应用程序。我没有时间就这个主题写一篇完整的论文,而且我没有涵盖很多细微差别,但希望这是有用的。
If you need your web app to act more like a mobile or desktop application, then single-page dynamic web app using a framework like AngularJS is one way to go.
如果您需要您的 Web 应用程序更像移动或桌面应用程序,那么使用 AngularJS 等框架的单页动态 Web 应用程序是一种方法。