java angular js 优于 jsp 视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31079695/
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
angular js benefits over jsp view
提问by hugh
I wonder why people are using angularjs to check things like existing username or form registration when it can be done simply by a presentation layer called jsp in combination with servlet as backend code.
我想知道为什么人们使用 angularjs 来检查诸如现有用户名或表单注册之类的事情,因为它可以简单地通过称为 jsp 的表示层结合 servlet 作为后端代码来完成。
For example, angular makes ajax calls to java servlet where servlet fetches username from DB and shown back to jsp view. All this can be achieved with jsp and servlet instead of adding one more layer of angular using separate custom directives and so on which adds to more code.
例如,angular 对 java servlet 进行 ajax 调用,其中 servlet 从 DB 获取用户名并显示回 jsp 视图。所有这一切都可以通过 jsp 和 servlet 来实现,而不是使用单独的自定义指令再添加一层 angular 等等,这会增加更多的代码。
I am trying to figure out advantages of angular over jsp view layer. Things like form registration can be done with jsp instead of angular.
我试图找出 angular 相对于 jsp 视图层的优势。诸如表单注册之类的事情可以用 jsp 而不是 angular 来完成。
Today, I spent almost 8 hours writing a program to check existing username using angular and java servlet being a back end. I am not sure if its a bug in angular or jsp where request.getParameter("Username")
returns null in servlet. The parameter is passed from angular from using $http
post ajax call.
今天,我花了将近 8 个小时编写一个程序来使用 angular 和 java servlet 作为后端检查现有用户名。我不确定它是否是 angular 或 jsp 中的错误,其中request.getParameter("Username")
在 servlet中返回 null。该参数是从 angular 使用$http
post ajax 调用传递的。
Finally I had to use some inputstream to read the value passed from angular form and return a response. Who can imagine this sort of issue?
最后我不得不使用一些输入流来读取从角度形式传递的值并返回一个响应。谁能想到这种问题?
Finally, my question is what makes angular better than jsp. Is there any other back end sever better to be used with angular (may be nodejs?) than tomcat usually used along with java?
最后,我的问题是什么使 angular 比 jsp 更好。有没有其他后端服务器比通常与 java 一起使用的 tomcat 更适合与 angular(可能是 nodejs?)一起使用?
回答by hugh
The biggest difference between Angular and JSP is where the work carried out - if you want a rich web application with lots of user interaction and page state, Angular may be the best choice. If you want pages with simple, fixed interactions and most of the work carried out server-side, JSP might suit your needs better. As with all these decisions, it's true that you could do this in many different technologies, it's just a matter of which best suits the task you're trying to accomplish. It's not really a matter of whether Angular is better, it's just more appropriate for some applications.
Angular 和 JSP 之间最大的区别在于工作执行的位置——如果您想要一个具有大量用户交互和页面状态的丰富 Web 应用程序,Angular 可能是最佳选择。如果您希望页面具有简单、固定的交互并且大部分工作在服务器端进行,那么 JSP 可能更适合您的需求。与所有这些决定一样,您确实可以在许多不同的技术中做到这一点,这只是一个最适合您尝试完成的任务的问题。Angular 是否更好并不是真正的问题,它只是更适合某些应用程序。
回答by Robert Moskal
If you are making a single page web application then the benefits of using a front end web framework like angular or backbone are clear. They handle client side routing, templating, data binding, dependency injection ajax interaction with the back end, etc.. etc. Vanilla jsp has no facilities for building SPAs at all.
如果您正在制作单页 Web 应用程序,那么使用 angular 或 Backbone 等前端 Web 框架的好处是显而易见的。它们处理客户端路由、模板、数据绑定、与后端的依赖注入 ajax 交互等。Vanilla jsp 根本没有构建 SPA 的工具。
Angular really wants to talk to the backend using something like a RESTFUL api. To make your life easier, I suggest augmenting your servlet application with JAX-RS implementation like jersey or restEasy. These will give you a more natural and idiomatic way to communicate with angular.
Angular 真的很想使用 RESTFUL api 之类的东西与后端对话。为了让您的生活更轻松,我建议您使用 JAX-RS 实现(如 jersey 或 restEasy)来扩充您的 servlet 应用程序。这些将为您提供一种更自然和惯用的方式来与 angular 进行交流。
It's still going to be a bit more work that using node (but hey you're used to that, you're using java). But you'll find it more productive than using naked servlets.
使用 node 仍然需要更多的工作(但是你已经习惯了,你正在使用 java)。但是您会发现它比使用裸 servlet 更有效率。