开发者

RESTful WebService: How does a server send both View & JSON/XML?

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:

RESTful WebService: How does a server send both View & JSON/XML?

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


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜