開発者ドキュメント

Spring 3 MVCとRSSフィードの例

Spring 3では、java.netのROMEパッケージを使用して、RSSフィードビューを生成する抽象クラス ”

AbstractRssFeedView

“が提供されています。このチュートリアルでは、Spring MVCフレームワークからRSSフィードビューを生成する方法を説明します。

使用される技術:

  1. Spring 3.0.5.RELEASE

  2. ROME 1.0.0

  3. JDK 1.6

  4. Eclipse 3.6

  5. Maven 3

チュートリアルの最後に、このURL(


http://localhost:8080/SpringMVC/rest/rssfeed


)にアクセスすると、ブラウザは次のRSSフィードコンテンツを返します。

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
  <channel>
    <title>Mkyong Dot Com</title>
    <link>/</link>
    <description>Java Tutorials and Examples</description>
    <item>
      <title>Spring MVC Tutorial 1</title>
      <link>//spring-mvc/tutorial-1</link>
      <content:encoded>Tutorial 1 summary ...</content:encoded>
      <pubDate>Tue, 26 Jul 2011 02:26:08 GMT</pubDate>
    </item>
    <item>
      <title>Spring MVC Tutorial 2</title>
      <link>//spring-mvc/tutorial-2</link>
      <content:encoded>Tutorial 2 summary ...</content:encoded>
      <pubDate>Tue, 26 Jul 2011 02:26:08 GMT</pubDate>
    </item>
  </channel>
</rss>

1.ディレクトリ構造

最終的なプロジェクトの構造を見直します。



プロジェクトの依存関係

Mavenの場合、 `pom.xml`の依存関係を以下のように宣言します。

    <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
    </properties>

    <dependencies>

        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- RSS -->
        <dependency>
            <groupId>net.java.dev.rome</groupId>
            <artifactId>rome</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!-- for compile only, your container should have this -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

3.モデル

単純なPOJOは、後でこのオブジェクトを使ってRSSフィードのコンテンツを生成します。

package com.mkyong.common.model;

import java.util.Date;

public class SampleContent {

    String title;
    String url;
    String summary;
    Date createdDate;

   //getter and seeter methods
}

4. AbstractRssFeedView

package com.mkyong.common.rss;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.feed.AbstractRssFeedView;
import com.mkyong.common.model.SampleContent;
import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Content;
import com.sun.syndication.feed.rss.Item;

public class CustomRssViewer extends AbstractRssFeedView {

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

        feed.setTitle("Mkyong Dot Com");
        feed.setDescription("Java Tutorials and Examples");
        feed.setLink("/");

        super.buildFeedMetadata(model, feed, request);
    }


    @Override
    protected List<Item> buildFeedItems(Map<String, Object> model,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

        @SuppressWarnings("unchecked")
        List<SampleContent> listContent = (List<SampleContent>) model.get("feedContent");
        List<Item> items = new ArrayList<Item>(listContent.size());

        for(SampleContent tempContent : listContent ){

            Item item = new Item();

            Content content = new Content();
            content.setValue(tempContent.getSummary());
            item.setContent(content);

            item.setTitle(tempContent.getTitle());
            item.setLink(tempContent.getUrl());
            item.setPubDate(tempContent.getCreatedDate());

            items.add(item);
        }

        return items;
    }

}

5.コントローラー

Spring MVCコントローラクラスで、rssフィードのコンテンツを生成し、ビュー名 ”

rssViewer

“を返します(このビュー名は上記の ”

CustomRssViewer

“に属し、後で手順6で登録します)。

package com.mkyong.common.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.mkyong.common.model.SampleContent;

@Controller
public class RssController {

    @RequestMapping(value="/rssfeed", method = RequestMethod.GET)
    public ModelAndView getFeedInRss() {

        List<SampleContent> items = new ArrayList<SampleContent>();

        SampleContent content  = new SampleContent();
        content.setTitle("Spring MVC Tutorial 1");
        content.setUrl("//spring-mvc/tutorial-1");
        content.setSummary("Tutorial 1 summary ...");
        content.setCreatedDate(new Date());
        items.add(content);

        SampleContent content2  = new SampleContent();
        content2.setTitle("Spring MVC Tutorial 2");
        content2.setUrl("//spring-mvc/tutorial-2");
        content2.setSummary("Tutorial 2 summary ...");
        content2.setCreatedDate(new Date());
        items.add(content2);

        ModelAndView mav = new ModelAndView();
        mav.setViewName("rssViewer");
        mav.addObject("feedContent", items);

        return mav;

    }

}

6. Spring Beanの登録

Spring Bean定義ファイルで、自動コンポーネントスキャンを有効にし、 “` CustomRssViewer` “クラスと” BeanNameViewResolver “” viewリゾルバを登録して、ビュー名 ”

rssViewer

“が返されると、 Bean ID ”

rssViewer

“。

ファイル:mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.mkyong.common.controller"/>

    <!-- Map returned view name "rssViewer" to bean id "rssViewer" -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

    <bean id="rssViewer" class="com.mkyong.common.rss.CustomRssViewer"/>

</beans>
  • 注意** `web.xml`のファイル内容は省略されています。興味のある方は記事の最後にこのプロジェクト全体をダウンロードしてください。

7.デモ

「spring-mvc-rss-feed-demo」、width = 640 、高さ= 442]

  • Atomについてはどうですか?** + Atomでは、

    AbstractRssFeedView`の代わりに

    AbstractAtomFeedView`を拡張するだけです。

ソースコードをダウンロードする

ダウンロードする –

SpringMVC-RSS-Feed-Example.zip

(9 KB)

モバイルバージョンを終了