Today I wanted to have a different way of using my application besides the plain jsp pages. Suddenly it struck me, i wanted an rss feed. I fired up my friend google and found the project Rome. It did not look that hard by browsing the sample applications. I also found a sample how you could use a servlet. Oke, the step to a spring implementation was not that hard. This blog item will talk you through the creation of a new view in Spring mvc, an RSS feed.

First you need to download Rome and ,a href=”http://www.jdom.org”>jdom. After downloading them, add the jars to the lib folder of your web project. (I am not going to discuss in detail how to setup your spring web mvc project, I have used the equinox project as a startup)

Before we can create a class that generates an rss feed, we need to configure spring. We must be able to find the view class.

  • Add a urlMapping to the spring config file
  • <prop key=”/rss.html”>rssController</prop>

  • Add the rssController bean, in my case this was just a controller that creates a java.util.List with orders and places it on the request
  • — action-servlet.xml —
    <bean id=”rssController” class=”nl.primatras.backoffice.web.OrderController”>
      <property name=”toView” value=”rss”/>
      <property name=”numOrders” value=”25″/>
      <property name=”orderManager” ref=”orderManager”/>
    </bean>

    — OrderController.java —
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
      List orders = orderManager.listOrders(0,getNumOrders());
      return new ModelAndView(this.getToView(),”orders”,orders);
    }

  • Add a viewResolver to the views.properties file
  • rss.class=nl.primatras.backoffice.web.view.RssOrderView

  • Implement the View class, this is discussed in the remainder of this blog item. The class I created RssOrderView is a subclass of springs AbstractView

Creating a feed with Rome is as simple as instantiating a new Feed object, setting the rss type and the content type, creating a feed output, and outputting the output. See the next code for the steps.

SyndFeed feed = getFeed(orders,baseUrl); // use a private method to get the feed

String feedType = request.getParameter(FEED_TYPE);
feedType = (feedType!=null) ? feedType : DEFAULT_FEED_TYPE;
feed.setFeedType(feedType); // set the type of the rss feed to output

response.setContentType(getContentType()); // Set the content type of the response object to be returned to the clinet.

SyndFeedOutput output = new SyndFeedOutput(); // Create the outputter
output.output(feed,response.getWriter()); // write the feed to the output

A feed consists of a few properties like: author, title, description, etc. It also contains a List with items. These items are called entries. For my sample I created the entries based on a list of orders I obtained through the OrderController by taking the list from the request. Each order becomes an entry. Some important parts are below.

SyndFeed feed = new SyndFeedImpl();
feed.setAuthor(getMessageSourceAccessor().getMessage(“order.feed.author”));
feed.setTitle(getMessageSourceAccessor().getMessage(“order.feed.title”));
feed.setDescription(getMessageSourceAccessor().getMessage(“order.feed.description”));
feed.setLink(contextPath);

List entries = new ArrayList();
for (Order order : orders) {
  String entryTitle = order.getCustomer().getLastname();
  String entryLink = contextPath + getOrderOverviewView() +
    “?id=” + order.getId();
  String entryDescription = order.toString();

  entries.add(createEntry(entryTitle,entryLink,entryDescription,order.getCreatedate()));
}

feed.setEntries(entries);

And finally the method to create an entry based on an order:

private SyndEntry createEntry (String title, String link, String description,
    Date createDate) {
  SyndEntry entry = new SyndEntryImpl();
  entry.setTitle(title);
  entry.setLink(link);
  entry.setPublishedDate(createDate);

  SyndContent entryDescription = new SyndContentImpl();
  entryDescription.setType(“text/plain”);
  entryDescription.setValue(description);

  entry.setDescription(entryDescription);

  return entry;
}

I hope this will help some of you to create a spring view object and start your own rss feed. You can download the complete class here

An improved base class for this class is now included in the Springmodules project. You can find it here

Creating an rss feed with spring and Rome