law_keven
Photo credit: law_keven
del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | 3 | Next »
Software Development

Publish an RSS Feed with Spring 3.0

Use Spring 3.0's new AbstractRssFeedView class to publish an RSS feed.

At the time of this writing, Spring 3.0 isn't out yet, but that doesn't mean we can't start goofing around with the nightly snapshots. I've been doing just that, and in this article I'm going to show you how to publish an RSS feed using the new AbstractRssFeedView class from Spring 3.0.

Those familiar with Spring Modules might recognize our view class. It began life as AbstractRssView in the Spring Modules project. But as of Spring 3.0, it's now a first-class member of the framework (though it's been renamed to AbstractRssFeedView), along with the AbstractAtomFeedView class for Atom feeds, and the AbstractFeedView, which serves as a base class for both. The new classes, like the old one, are based on Sun's ROME API.

You'll need to know Spring Web MVC to get the most out of this article. In particular, you'll need to understand the relationship between controllers and views, which in a nutshell is this: when a controller is done processing a request, it returns a logical view name that a view resolver subsequently maps to a view.

Without further ado, let's look at some code.

The controller

Let's start with the controller, since processing starts there, and since that's probably more familiar to more readers than implementing views. Listing 1 shows a pretty basic controller for a news feed.

Listing 1. Our really simple controller
package rssdemo.web;

import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import rssdemo.model.NewsItem;
import rssdemo.service.NewsService;

@Controller
public final class NewsController {
    private NewsService newsService;
    private String rssNewsFeedView;
    
    @Required
    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }
    
    @Required
    public void setRssNewsFeedView(String rssNewsFeedView) {
        this.rssNewsFeedView = rssNewsFeedView;
    }
    
    @RequestMapping("/news.rss")
    public String rss(HttpServletResponse res, Model model) {
        List<NewsItem> newsItems = newsService.getAllNewsItems();
        model.addAttribute(newsItems);                                     // 1
        return rssNewsFeedView;                                            // 2
    }
}

As you can see, this controller really is as simple as I said it would be. Our rss() method grabs a list of NewsItems from the NewsService 1 and places it on the Model under a conventionally-generated key, which in this case would be newsItemList. Then we simply return an injected logical view name 2, which we're going to map to an actual view shortly. (We inject the view name to maintain the separation between the controller and the view.)

Now let's check out the star of the show, which would be our view.

Social bookmarks: del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | 3 | Next »

Comments (4)

Very nice article. Thanks heaps.

By Adrian on Jul 20, 2010 at 6:58 AM PDT

The Pandora pandora schmuck myth first Pandora Armreifenappears in lines Pandora Halsketten of Hesiod's poem in Pandora Charms epic meter, the Theogony (ca. 8th?7th centuries BC), without ever giving the woman a name. After humans Pandora Sets have received thethe myth is a rosetta stone kind of theodicy, addressing the question pop information, web easy get, sports fashion, news-fashionof why there is evil in the world. In the seventh hot-winter century BC, Hesiod, both in his Theogony (briefly, without naming Pandora outright rosetta stone language, rosetta stone spanish, abercrombie and fitch, Abercrombie Fitch

By pandora schmuck on Aug 30, 2010 at 11:44 PM PDT

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Preview:
By You
Please help us reduce comment spam:
Spring in Practice
My brother and I are writing Spring in Practice for Manning!

What's New?

2009-08-30 - Check out my two-part series on DZone: Spring Integration: A Hands-On Tutorial.
2009-03-25 - My new article Getting Started with Spring Batch 2.0 is available on DZone.
Home | Consulting | Tech Articles | Mailing List | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.