RESTful WebService:服务器如何发送视图和视图? JSON/XML?

发布于 2024-10-19 04:15:58 字数 885 浏览 2 评论 0原文

我之前编写过一些小型的 RESTful Web 服务 (RWS)。但在这些情况下,可以完全控制视图(表示层),即视图是平台(智能手机?)上本地运行的应用程序。在发送 JSON(或文本或任何方便的表示形式,让我们假设 JSON 仅适用于该主题)的服务器上,对视图和 RWS 进行独立控制。

现在进入网络:视图(即 HTML 页面)驻留在服务器上。该服务器现在应该提供 HTML 和 JSON。我的问题是两者是如何分离(或耦合)的?这是一个例子: 在此处输入图像描述

问题:

  1. 在图像中的步骤 (X) 中,当墙页面返回给客户端,所有墙帖子都填充在该页面上。如果客户端的视图不是由服务器提供的,它可能只会返回墙贴的 JSON。那么本案中这种情况是如何处理的呢?服务器是否应该返回具有所有呈现/格式化逻辑的服务器端页面 (SSP)?

  2. 在步骤 (Y),用户希望更新页面上的某些内容,并向服务器发送 jQuery+Ajax HTTP:PUT(在某个 URI 处,因此墙页面是一个外观?)。

困惑(==问题?:-)

  • 当请求发送到服务器时,如何分离 JSON + SSP 的关注点?

  • 这就是基于网络的客户端的设计方式吗?返回的第一个页面 (X) 实际上是一个 SSP,其中包括对服务器进行 Ajax/REST 调用的所有逻辑??

  • 如何构建一个好的页面,即 JSP(例如)+ jquery + CSS + AJAX? (在这种情况下是否有可能没有 SSP 设计?即只有 HTML + jquery + CSS?)

只是有点困惑..

提前致谢

I've coded a few, albeit small RESTful Web Services (RWS) before. But In those cases there was total control over the view (presentation layer) i.e., the view was a locally running application on the platform (smartphone?). There was independent control of the view and the RWS at the server that would send JSON (or text or whatever representation that was convenient, let's assume JSON only for the topic).

Now coming to the web: The view (i.e., HTML pages) reside on a server. That server is now supposed to serve the HTML as well as the JSON. My question is how are the 2 separated (or coupled)? Here is an example:
enter image description here

Questions:

  1. At step (X) in the image when the wall page is returned to the client all wall posts are populated on that page. If it were a client whose view was not supplied by a server it'd probably just return JSON of wall posts. So how is this situation handled in this case? Should the server return a server side page (SSP) that has all the rendering/formatting logic?

  2. At step (Y) the user wishes to update something on the page and sends a jQuery+Ajax HTTP:PUT to the server (at some URI, so the wall page is a facade?).

Confusions (== Questions ? :-)

  • How do you separate the concerns of JSON + SSP when a request is sent to the server?

  • Is this how web-based clients are designed?? The first page returned (X) is actually a SSP which includes all the logic for making Ajax/REST calls to the server??

  • How does one then go about a good page construction i.e., JSP (say) + jquery + CSS + AJAX?? (Is it possible to have a NO SSP design in this case? i.e. only HTML + jquery + CSS??)

Just a bit confused..

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶情人眼里出诗心の 2024-10-26 04:15:58

我想我不太明白你在这里需要什么。

但为什么不简单地查看请求的 Content-Type 标头并返回客户端所要求的内容呢?如果他们要求 HTML,请向他们发送 HTML。如果他们要求 JSON,请向他们发送 JSON。

您可能需要考虑像 JAX-RS 这样的东西,它会根据 Content-Type 分派代码,也许这会减轻您的一些痛苦。

I guess I'm not understanding exactly what you need here.

But why not simply look at the request Content-Type header and return the client what they ask? If they ask for HTML, send them HTML. If they ask for JSON, send them JSON.

You might want to consider something like JAX-RS which will dispatch to code based on the Content-Type, perhaps that will relieve some of your pain.

帅气称霸 2024-10-26 04:15:58

网络浏览器只是一个渲染引擎。它只会渲染并执行 Web 服务器发送给它的内容。
在你的情况下,你必须发送浏览器html,无论该html是静态的还是在服务器上生成的,与浏览器无关。
当浏览器呈现 html 时,它还会运行您指定的任何 js/css 代码。

* Is this how web-based clients are designed?? The first page returned (X) is actually a SSP which includes all the logic for making Ajax/REST calls to the server??

是的,您发送到浏览器的 html 中包含 js 代码,该代码告诉浏览器在发生某些事件(例如单击按钮或页面加载)时该怎么做。

* How does one then go about a good page construction i.e., JSP (say) + jquery + CSS + AJAX?? (Is it possible to have a NO SSP design in this case? i.e. only HTML + jquery + CSS??)

是的,你可以提供一个静态 html 页面,其中包含所有正确的 js,浏览器就会执行它的操作。

我希望这能回答您的问题?

The web browser is just a rendering engine. It will only render and execute what the web server sends it.
In your situation, you have to send the browser html, whether that html is static or generated on the server, is irrelevant to the browser.
When the browser renders the html, it also runs any js/css code that you have specified.

* Is this how web-based clients are designed?? The first page returned (X) is actually a SSP which includes all the logic for making Ajax/REST calls to the server??

Yes, the html you send to the browser has js code that tells the browser what to do when certain events happen, like a button click or page load.

* How does one then go about a good page construction i.e., JSP (say) + jquery + CSS + AJAX?? (Is it possible to have a NO SSP design in this case? i.e. only HTML + jquery + CSS??)

yes, you can just serve up a static html page that has all correct js in it, and the browser will do its thing.

I hope that answers your questions?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文