Spring MVC &休息:不同的“布局”具有相同的“内容”?
我有一个 Spring 3.0 Web 应用程序,只要实用,它就会尝试遵循 REST 原则。
我有一个控制器方法来返回文件夹(这是我的业务实体)。 (获取 http://.../folders/{id})。如果用户访问此 URL,他将获得完整的 HTML 页面:包含页眉、页脚、菜单和文件夹详细信息。 – 页眉、页脚和菜单的“增强”是使用 Apache Tiles 完成的。
现在我有另一个视图,用户可以在其中看到文件夹树,如果他单击其中一个文件夹,文件夹详细信息将/应该通过 AJAX 加载,并显示在树旁边。在这种情况下,AJAX 响应应该是呈现的 HTML(不是 JSON),因为真实内容应该看起来像用户直接访问文件夹页面时一样。
我的问题是,当我请求相同的 URL 时,服务器不仅会返回真实内容,还会返回页眉、页脚和菜单。
我的问题是如何以时尚的方式处理这个问题: – 当然,我可以使用一个附加参数和两个具有不同图块模板的控制器方法,但我认为这不太好,因为它使用 html 参数对于“布局”,我必须编写两个控制器方法(以及周围的所有内容)才能有两个图块模板
- 那么以休息方式使用另一个 HTML“布局”访问相同“内容”的推荐方法是什么?
- 有没有什么方法可以根据我的需要来切换图块模板定义中的模板(如何指定取决于其他问题的答案)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 ReST 中,像您所描述的问题将通过基于 Accept 标头的内容协商来解决:请求者将指示预期的类型,例如 test/html、application/json 等。您可以通过以下方式使用它:让您的 Ajax 调用请求不同的类型。由于您使用的是 Spring 3 MVC,因此您应该能够配置 ViewResolvers 以根据请求的类型返回装饰或未装饰的视图。这看起来像一个合理的示例。
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.
为了让基于服务器的应用程序发送不同的内容,您需要为页面的“完整版本”和“真实内容”请求不同的 URL。这是不可避免的。
如果您担心重复代码,那么我只需设计主布局,使其使用与单击其中一个文件夹时相同的机制加载页面的内部详细信息,而不是在控制器呈现该数据时加载该数据看法。
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.
通过 REST,您可以使用以下选项
/resource?layout=x
text/html ;layout=x
,并从接受标头中请求Prefer: return=minimal
另一种可能的解决方案具有可重用模板并仅发送 JSON 数据。
(REST 有很多限制,因此您的 Web 服务可能不是真正的 REST 服务。)
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.)