博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 3 MVC And RSS Feed Example
阅读量:6417 次
发布时间:2019-06-23

本文共 5620 字,大约阅读时间需要 18 分钟。

In Spring 3, comes with a abstract class “AbstractRssFeedView” to generate RSS feed view, using java.net’s ROME package. In this tutorial, we show you how to generate a RSS feed view from Spring MVC framework.

Technologies used :

  1. Spring 3.0.5.RELEASE
  2. ROME 1.0.0
  3. JDK 1.6
  4. Eclipse 3.6
  5. Maven 3

At the end of the tutorial, when you visit this URL – http://localhost:8080/SpringMVC/rest/rssfeed, browser will return following RSS feed content :

<?
xml version="1.0" encoding="UTF-8"
?>
<
rss 
xmlns:content
="http://purl.org/rss/1.0/modules/content/"
 version
="2.0"
>
  
<
channel
>
    
<
title
>Mkyong Dot Com
</
title
>
    
<
link
>http://www.mkyong.com
</
link
>
    
<
description
>Java Tutorials and Examples
</
description
>
    
<
item
>
      
<
title
>Spring MVC Tutorial 1
</
title
>
      
<
link
>http://www.mkyong.com/spring-mvc/tutorial-1
</
link
>
      
<
content:encoded
>Tutorial 1 summary ...
</
content:encoded
>
      
<
pubDate
>Tue, 26 Jul 2011 02:26:08 GMT
</
pubDate
>
    
</
item
>
    
<
item
>
      
<
title
>Spring MVC Tutorial 2
</
title
>
      
<
link
>http://www.mkyong.com/spring-mvc/tutorial-2
</
link
>
      
<
content:encoded
>Tutorial 2 summary ...
</
content:encoded
>
      
<
pubDate
>Tue, 26 Jul 2011 02:26:08 GMT
</
pubDate
>
    
</
item
>
  
</
channel
>

</rss> 

1. Directory Structure

Review the final project structure.

directory structure

 

2. Project Dependencies

For Maven, declares following dependencies in your pom.xml.

<
properties
>
        
<
spring.version
>3.0.5.RELEASE
</
spring.version
>
    
</
properties
>
 
    
<
dependencies
>
 
        
<!--
 Spring 3 dependencies 
-->
        
<
dependency
>
            
<
groupId
>org.springframework
</
groupId
>
            
<
artifactId
>spring-core
</
artifactId
>
            
<
version
>${spring.version}
</
version
>
        
</
dependency
>
 
        
<
dependency
>
            
<
groupId
>org.springframework
</
groupId
>
            
<
artifactId
>spring-web
</
artifactId
>
            
<
version
>${spring.version}
</
version
>
        
</
dependency
>
 
        
<
dependency
>
            
<
groupId
>org.springframework
</
groupId
>
            
<
artifactId
>spring-webmvc
</
artifactId
>
            
<
version
>${spring.version}
</
version
>
        
</
dependency
>
 
        
<!--
 RSS 
-->
        
<
dependency
>
            
<
groupId
>net.java.dev.rome
</
groupId
>
            
<
artifactId
>rome
</
artifactId
>
            
<
version
>1.0.0
</
version
>
        
</
dependency
>
 
        
<!--
 for compile only, your container should have this 
-->
        
<
dependency
>
            
<
groupId
>javax.servlet
</
groupId
>
            
<
artifactId
>servlet-api
</
artifactId
>
            
<
version
>2.5
</
version
>
            
<
scope
>provided
</
scope
>
        
</
dependency
>
 

    </dependencies> 

3. Model

A simple POJO, later use this object to generate the RSS feed content.

package com.mkyong.common.model;
 
import java.util.Date;
 
public 
class SampleContent {
 
    String title;
    String url;
    String summary;
    Date createdDate;
 
    
//
getter and seeter methods

4. AbstractRssFeedView

Create a class extends AbstractRssFeedView, and override the buildFeedMetadata and buildFeedItems methods, below code should be self-explanatory.

package com.mkyong.common.rss;
 
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.mkyong.common.model.SampleContent;
import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Content;
import com.sun.syndication.feed.rss.Item;
 
public 
class CustomRssViewer 
extends AbstractRssFeedView {
 
    @Override
    
protected 
void buildFeedMetadata(Map<String, Object> model, Channel feed,
        HttpServletRequest request) {
 
        feed.setTitle("Mkyong Dot Com");
        feed.setDescription("Java Tutorials and Examples");
        feed.setLink("http://www.mkyong.com");
 
        
super.buildFeedMetadata(model, feed, request);
    }
 
 
    @Override
    
protected List<Item> buildFeedItems(Map<String, Object> model,
        HttpServletRequest request, HttpServletResponse response)
        
throws Exception {
 
        @SuppressWarnings("unchecked")
        List<SampleContent> listContent = (List<SampleContent>) model.get("feedContent");
        List<Item> items = 
new ArrayList<Item>(listContent.size());
 
        
for(SampleContent 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;
    }
 

6. Spring Bean Registration

In a Spring bean definition file, enable the auto component scanning, and register your “CustomRssViewer” class and “BeanNameViewResolver” view resolver, so that when view name “rssViewer” is returned, Spring know it should map to bean id “rssViewer“.

File : mvc-dispatcher-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.mkyong.common.controller"
 
/>
 
    
<!--
 Map returned view name "rssViewer" to bean id "rssViewer" 
-->
    
<
bean 
class
="org.springframework.web.servlet.view.BeanNameViewResolver"
 
/>
 
    
<
bean 
id
="rssViewer"
 class
="com.mkyong.common.rss.CustomRssViewer"
 
/>
 

</beans> 

Note

File content of web.xml is omitted, just a standard configuration, if you are interest, download this whole project at the end of the article.

7. Demo

URL : http://localhost:8080/SpringMVC/rest/rssfeed

spring mvc and rss feed demo
How about Atom?
For Atom, you just need to extends 
AbstractAtomFeedView, instead of 
AbstractRssFeedView.

Download Source Code

Download it –  (9 KB) 

转载地址:http://vbpra.baihongyu.com/

你可能感兴趣的文章
java线程同步原理(lock,synchronized)
查看>>
MyEclipse中使用Hql编辑器找不到Hibernate.cfg.xml文件解决方法
查看>>
yRadio以及其它
查看>>
第四节 对象和类
查看>>
闪迪(SanDisk)U盘防伪查询(官方网站)
查看>>
Android onMeasure方法介绍
查看>>
无锁数据结构
查看>>
MySQL的变量查看和设置
查看>>
android onNewIntent
查看>>
XML特殊符号
查看>>
kaptcha可配置项
查看>>
JavaMail邮箱验证用户注册
查看>>
系统时间——ntpd
查看>>
反射实现AOP动态代理模式(Spring AOP实现原理)
查看>>
Http协议与缓存
查看>>
监测超过特定内存阀值进程并结束
查看>>
Linux Centos 查询信息
查看>>
android adb命令
查看>>
python “双”稀疏矩阵转换为最小联通量“单”矩阵
查看>>
揭秘天猫双11背后:20万商家600万张海报,背后只有一个鹿班
查看>>