java 如何正确连接前端和后端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45413340/
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
How to connect frontend and backend correctly?
提问by A.Yam
I'm new in the web programming. So, i have some trouble with understanding how to connect front and back ends. I'm writing on java. So, I have spring mvc and jsp(or another template engine like Apache FreeMarker etc.) as view layer. How to integrate there reactjs or angular2+ fron-tend frameworks? And should I do that stuff? Is it actual to use java code in html (jsp) or ruby code in html (erb)? If it is actually, how to use front-end frameworks in this case?(Or it shouldn't be used?) May be I must create something like RESTful back-end and integrate it with angular 2(as an example)? May be there is other way to integrate them? Could You explain me how should i create my web app? Or share with me some links by this topic? If there are several way to do what i ask, could you, also, explain me advantages and disadvantages of each approach? And sorry for my English. Thank you in advance.
我是网络编程的新手。所以,我在理解如何连接前端和后端时遇到了一些麻烦。我在用java写。所以,我有 spring mvc 和 jsp(或其他模板引擎,如 Apache FreeMarker 等)作为视图层。如何集成 reactjs 或 angular2+ 前端框架?我应该做那些事吗?在 html (jsp) 中使用 java 代码或在 html (erb) 中使用 ruby 代码是否实际?如果确实如此,在这种情况下如何使用前端框架?(或者不应该使用它?)可能我必须创建类似 RESTful 后端的东西并将其与 angular 2 集成(例如)?可能有其他方法来整合它们?你能解释一下我应该如何创建我的网络应用程序吗?或者与我分享这个主题的一些链接?如果有几种方法可以按照我的要求做,你也可以,请解释一下每种方法的优缺点?对不起我的英语。先感谢您。
回答by Idriss Benbassou
Frontend: things that run in the browser, that's based on HTML/CSS/JS
前端:在浏览器中运行的东西,基于 HTML/CSS/JS
Backend: things that run on the server, Ruby, PHP, Python, Java, JavaScript/Node, bash scripts, DBMS
后端:在服务器上运行的东西,Ruby、PHP、Python、Java、JavaScript/Node、bash 脚本、DBMS
Front-end and back-end interact in two ways:
前后端交互有两种方式:
1- A simple blog case: When you type the URL of a specific post, your browser is making a HTTP request to the back-end. The back-end returns a HTTP response containing the HTML code interpreted by the browser. The only interaction between the two edges is indeed the HTTP flow.
1- 一个简单的博客案例:当您输入特定帖子的 URL 时,您的浏览器正在向后端发出 HTTP 请求。后端返回一个 HTTP 响应,其中包含浏览器解释的 HTML 代码。两条边之间唯一的交互确实是 HTTP 流。
2- A single-page, AJAX-powered application: It's even more clear here that the only interaction is HTTP. The Javascript-powered front-end UI (or it could be any web, mobile or desktop application actually) sends HTTP requests to the back-end in order to read, create, update or delete data. The back-end returns HTTP responses containing the necessary information for the UI/webapp to change its state, display data to the user, etc...
2- 单页、AJAX 驱动的应用程序:这里更清楚的是,唯一的交互是 HTTP。由 Javascript 驱动的前端 UI(或者实际上可以是任何 Web、移动或桌面应用程序)向后端发送 HTTP 请求,以便读取、创建、更新或删除数据。后端返回 HTTP 响应,其中包含 UI/web 应用程序更改其状态、向用户显示数据等所需的信息......