Spring MVC & Rest: different “layout” with same “content”?

扶醉桌前 提交于 2019-12-10 19:40:55

问题


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).

回答1:


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.




回答2:


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.




回答3:


By REST you have the following options

  • define a new IRI to the resource, for example: /resource?layout=x
  • add a custom MIME type (or MIME param), for example text/html;layout=x, and ask for that from the accept header
  • send a prefer header e.g. Prefer: 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.)



来源:https://stackoverflow.com/questions/6664857/spring-mvc-rest-different-layout-with-same-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!