Spring MVC-集成(Integration)-生成RSS源示例(转载实践)

以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_rss_feed.htm

说明:示例基于Spring MVC 4.1.6

以下示例显示如何使用Spring Web MVC框架生成RSS Feed。首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态窗体的Web应用程序:

步骤描述
1 创建一个名为TestWeb的项目,在一个包com.tutorialspoint下,如Spring MVC - Hello World Example章节所述。
2 在com.tutorialspoint包下创建Java类RSSMessage,RSSFeedViewer和RSSController。
3 从同一个maven存储库页面下载Rome library Rome及其依赖项rome-utils,jdom和slf4j。把它们放在你的CLASSPATH中。
4 在src文件夹下创建一个属性文件messages.properties。
5 最后一步是创建所有源和配置文件的内容并导出应用程序,如下所述。

RSSMessage.java

package com.tutorialspoint;

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

public 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> 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

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

这里我们创建了一个RSS feed POJO RSSMessage和一个RSS消息查看器,它扩展了AbstractRssFeedView并覆盖了它的方法。在RSSController中,我们生成了一个示例RSS源。

完成创建源文件和配置文件后,导出应用程序。右键单击应用程序并使用Export > WAR File选项,并将您的TestWeb.war文件保存在Tomcat的webapps文件夹中。

现在启动您的Tomcat服务器,并确保您可以使用标准浏览器从webapps文件夹访问其他网页。现在尝试URL http://localhost:8080/TestWeb/rssfeed,您应该看到以下结果。

Maven示例:

https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test27

posted @ 2017-09-10 10:38  EasonJim  阅读(460)  评论(0编辑  收藏  举报