Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC & Rest: different "layout" with same "content"?

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

  • So what is a recommended way to access the same "content" with another HTML "layout" in a rest way?
  • Is there any way to switch the template in the tiles template definition, depending on what I need (how that is specified depends on the answer to the other question).
like image 813
Ralph Avatar asked Mar 07 '26 14:03

Ralph


2 Answers

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.

like image 178
Ryan Stewart Avatar answered Mar 09 '26 02:03

Ryan Stewart


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.

like image 28
matt b Avatar answered Mar 09 '26 04:03

matt b



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!