Spring MVC生成RSS源


下面的範例演示如何使用Spring Web MVC框架生成RSS源。 首先使用Eclipse IDE,並按照以下步驟使用Spring Web Framework開發基於動態表單的Web應用程式:

  1. 建立一個名稱為 GenerateRSSFeed 的動態WEB專案。
  2. com.yiibai.springmvc 包下建立三個Java類:RSSMessage, RSSFeedViewerRSSController
  3. 從相同的maven儲存庫頁面下載 Rome 庫及其依賴項rome-utilsjdomslf4j。和所需的依賴關係存並將它們放在CLASSPATH中。
  4. src檔案夾下建立屬性檔案messages.properties
  5. 最後一步是建立所有源和組態檔案的內容並執行應用程式,詳細如下所述。

完整的專案檔案目錄結構如下所示 -

RSSMessage.java 的程式碼如下所示 -

package com.yiibai.springmvc;

import java.util.Date;

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

RSSFeedViewer.java 的程式碼如下所示 -

package com.yiibai.springmvc;

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;

public class RSSFeedViewer extends AbstractRssFeedView {

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

      feed.setTitle("YiibaiPoint Dot Com");
      feed.setDescription("Java Yiibai and Examples");
      feed.setLink("https://www.tw511.com");

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

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

      List<RSSMessage> listContent = (List<RSSMessage>) model.get("feedContent");
      List<Item> items = new ArrayList<Item>(listContent.size());

      for(RSSMessage 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;        
   }
}

RSSController.java 檔案中的程式碼如下所示 -

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
public class RSSController {
   @RequestMapping(value="/rssfeed", method = RequestMethod.GET)
   public ModelAndView getFeedInRss() {

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

      RSSMessage content  = new RSSMessage();
      content.setTitle("Spring教學");
      content.setUrl("http://www.yiibai/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.yiibai/spring_mvc");
      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;
   }
}

GenerateRSSFeed-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.yiibai.springmvc" />
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
    <bean id="rssViewer" class="com.yiibai.springmvc.RSSFeedViewer" />
</beans>

上面的程式碼中建立了一個RSS feed POJO RSSMessage和一個RSS Message Viewer,它擴充套件了AbstractRssFeedView並覆蓋了它的方法。 在RSSController中,生成了一個範例RSS Feed

完成建立源和組態檔案後,發布應用程式到Tomcat伺服器。

現在啟動Tomcat伺服器,當存取URL => http://localhost:8080/GenerateRSSFeed/rssfeed , 如果Spring Web應用程式沒有問題,應該看到以下結果: