Reputation: 120831
I have a Spring 3.0 web application which tries to follow the REST principles as long as it is pragmatic.
I have a controller method to return a folder (this are my Business Entities). (GET http://.../folders/{id}). If a user accesses this URL he gets a complete HTML page: with header, footer, menu, and the folder details. – The "enhancing" with header, footer and menu is done with Apache Tiles.
Now I have another view where the user see the folder tree, and if he clicks on one of the folders, the folder details will/should be loaded via AJAX, and displayed next to the tree. In this case the AJAX Response should be the rendered HTML (no JSON), because the real content should look like when the user access the folder page directly.
My problem is, when I request the same URL, then the server will return not only the real content, but also the header, footer and menu.
My question is how can I handle this in a stylish way: – Of course I can use an additional parameter and two controller methods with different tile templates, but I think is not nice, because it uses html parameter for “layout” and I have to write two controller methods (and all the stuff around) only to have two tile templates
Upvotes: 3
Views: 1006
Reputation: 26137
By REST you have the following options
/resource?layout=x
text/html;layout=x
, and ask for that from the accept headerPrefer: return=minimal
Another possible solution having reusable templates and sending JSON data only.
(REST have many constraints, so probably your web service is not a real REST service.)
Upvotes: 0
Reputation: 128899
In ReST, problems like the one you're describing would be solved by content negotiation based on an Accept header: the requester would indicate the expected type such as test/html, application/json, etc. You could possibly make use of this by having your Ajax call request a different type. Since you're using Spring 3 MVC, you should be able to configure your ViewResolvers to return decorated or undecorated views based on the requested type. This looks like a reasonable example.
Upvotes: 3
Reputation: 139961
In order for your server-based application to send different content, you need to request different URLs for the "full version" of the page and the "real content". There is no avoiding that.
If you are concerned about duplicating code, then I would just design the main layout such that it loads the interior details of the page using the same mechanism as when you click on one of the folders, rather than loading that data when the controller renders the view.
Upvotes: 1