RSS and Atom

`AbstractAtomFeedView`和`AbstractRssFeedView`均继承自`AbstractFeedView`基类并且分别用于提供 Atom 和 RSS Feed 视图。它们基于 ROME项目,并位于包`org.springframework.web.servlet.view.feed`中。

Both AbstractAtomFeedView and AbstractRssFeedView inherit from the AbstractFeedView base class and are used to provide Atom and RSS Feed views, respectively. They are based on ROME project and are located in the package org.springframework.web.servlet.view.feed.

AbstractAtomFeedView 要求您实现 buildFeedEntries() 方法,也可以选择覆盖 buildFeedMetadata() 方法(默认实现为空)。以下示例展示了如何执行此操作:

AbstractAtomFeedView requires you to implement the buildFeedEntries() method and optionally override the buildFeedMetadata() method (the default implementation is empty). The following example shows how to do so:

  • Java

  • Kotlin

public class SampleContentAtomView extends AbstractAtomFeedView {

	@Override
	protected void buildFeedMetadata(Map<String, Object> model,
			Feed feed, HttpServletRequest request) {
		// implementation omitted
	}

	@Override
	protected List<Entry> buildFeedEntries(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// implementation omitted
	}
}
class SampleContentAtomView : AbstractAtomFeedView() {

	override fun buildFeedMetadata(model: Map<String, Any>,
			feed: Feed, request: HttpServletRequest) {
		// implementation omitted
	}

	override fun buildFeedEntries(model: Map<String, Any>,
			request: HttpServletRequest, response: HttpServletResponse): List<Entry> {
		// implementation omitted
	}
}

与实现 AbstractRssFeedView 相关的要求类似,如下例所示:

Similar requirements apply for implementing AbstractRssFeedView, as the following example shows:

  • Java

  • Kotlin

public class SampleContentRssView extends AbstractRssFeedView {

	@Override
	protected void buildFeedMetadata(Map<String, Object> model,
			Channel feed, HttpServletRequest request) {
		// implementation omitted
	}

	@Override
	protected List<Item> buildFeedItems(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// implementation omitted
	}
}
class SampleContentRssView : AbstractRssFeedView() {

	override fun buildFeedMetadata(model: Map<String, Any>,
								feed: Channel, request: HttpServletRequest) {
		// implementation omitted
	}

	override fun buildFeedItems(model: Map<String, Any>,
			request: HttpServletRequest, response: HttpServletResponse): List<Item> {
		// implementation omitted
	}
}

buildFeedItems()buildFeedEntries() 方法传递 HTTP 请求(如果您需要访问区域设置)。只将 HTTP 响应传递用于设置 Cookie 或其他 HTTP 标头。方法返回后,会自动将 Feed 写入响应对象。

The buildFeedItems() and buildFeedEntries() methods pass in the HTTP request, in case you need to access the Locale. The HTTP response is passed in only for the setting of cookies or other HTTP headers. The feed is automatically written to the response object after the method returns.

有关创建 Atom 视图的示例,请参阅 Alef Arendsen 的 Spring Team 博客 entry

For an example of creating an Atom view, see Alef Arendsen’s Spring Team Blog entry.