English 中文(简体)
Spring MVC - Generate RSS Feed
  • 时间:2024-11-03

Spring MVC - Generate RSS Feed Example


Previous Page Next Page  

The following example shows how to generate RSS Feed using the Spring Web MVC Framework. To start with, let us have a working Ecppse IDE in place and then consider the following steps to develop a Dynamic Form based Web Apppcation using the Spring Web Framework.

Step Description
1 Create a project with the name TestWeb under a package com.tutorialspoint as explained in the Spring MVC - Hello World chapter.
2 Create Java classes RSSMessage, RSSFeedViewer and RSSController under the com.tutorialspoint package.
3 Download the Rome pbrary Rome and its dependencies rome-utils, jdom and slf4j from the same maven repository page. Put them in your CLASSPATH.
4 Create a properties file messages.properties under the SRC folder.
5 The final step is to create the content of the source and configuration files and export the apppcation as explained below.

RSSMessage.java

package com.tutorialspoint;

import java.util.Date;

pubpc class RSSMessage {
   String title;
   String url;
   String summary;
   Date createdDate;
   pubpc String getTitle() {
      return title;
   }
   pubpc void setTitle(String title) {
      this.title = title;
   }
   pubpc String getUrl() {
      return url;
   }
   pubpc void setUrl(String url) {
      this.url = url;
   }
   pubpc String getSummary() {
      return summary;
   }
   pubpc void setSummary(String summary) {
      this.summary = summary;
   }
   pubpc Date getCreatedDate() {
      return createdDate;
   }
   pubpc void setCreatedDate(Date createdDate) {
      this.createdDate = createdDate;
   }	
}

RSSFeedViewer.java

package com.tutorialspoint;

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.rometools.rome.feed.rss.Channel;
import com.rometools.rome.feed.rss.Content;
import com.rometools.rome.feed.rss.Item;

pubpc class RSSFeedViewer extends AbstractRssFeedView {

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

      feed.setTitle("TutorialsPoint Dot Com");
      feed.setDescription("Java Tutorials and Examples");
      feed.setLink("http://www.tutorialspoint.com");

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

   @Override
   protected List<Item> buildFeedItems(Map<String, Object> model,
      HttpServletRequest request, HttpServletResponse response) throws Exception {
   
      List<RSSMessage> pstContent = (List<RSSMessage>) model.get("feedContent");
      List<Item> items = new ArrayList<Item>(pstContent.size());

      for(RSSMessage tempContent : pstContent ){

         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;		
   }
}

RSSController.java

package com.tutorialspoint;

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;

@Controller
pubpc class RSSController {
   @RequestMapping(value="/rssfeed", method = RequestMethod.GET)
   pubpc ModelAndView getFeedInRss() {

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

      RSSMessage content  = new RSSMessage();
      content.setTitle("Spring Tutorial");
      content.setUrl("http://www.tutorialspoint/spring");
      content.setSummary("Spring tutorial summary...");
      content.setCreatedDate(new Date());
      items.add(content);

      RSSMessage content2  = new RSSMessage();
      content2.setTitle("Spring MVC");
      content2.setUrl("http://www.tutorialspoint/springmvc");
      content2.setSummary("Spring MVC tutorial summary...");
      content2.setCreatedDate(new Date());
      items.add(content2);

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

      return mav;
   }
}

TestWeb-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.tutorialspoint" />

   <bean class = "org.springframework.web.servlet.view.BeanNameViewResolver" />

   <bean id = "rssViewer" class = "com.tutorialspoint.RSSFeedViewer" />
</beans>

Here, we have created a RSS feed POJO RSSMessage and a RSS Message Viewer, which extends the AbstractRssFeedView and overrides its method. In RSSController, we have generated a sample RSS Feed.

Once you are done with creating source and configuration files, export your apppcation. Right cpck on your apppcation, use Export → WAR File option and save the TestWeb.war file in Tomcat s webapps folder.

Now, start your Tomcat server and make sure you are able to access other webpages from the webapps folder using a standard browser. Try a URL − http://localhost:8080/TestWeb/rssfeed and we will see the following screen.

Spring RSS Generation Advertisements