java rss源_Spring MVC生成RSS源

本篇文章帮大家学习Spring MVC生成RSS源,包含了Spring MVC生成RSS源使用方法、操作技巧、实例演示和注意事项,有一定的学习价值,大家可以用来参考。

下面的示例演示如何使用Spring Web MVC框架生成RSS源。 首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:创建一个名称为 GenerateRSSFeed 的动态WEB项目。

在 com.yiibai.springmvc 包下创建三个java类:RSSMessage, RSSFeedViewer 和 RSSController。

从相同的maven存储库页面下载 Rome 库及其依赖项rome-utils,jdom和slf4j。和所需的依赖关系存并将它们放在CLASSPATH中。

在src文件夹下创建属性文件messages.properties。

最后一步是创建所有源和配置文件的内容并运行应用程序,详细如下所述。

完整的项目文件目录结构如下所示 -

55c28c52cff69d82b3f14f57bf3fd523.png

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 model, Channel feed,

HttpServletRequest request) {

feed.setTitle("YiibaiPoint Dot Com");

feed.setDescription("Java Yiibai and Examples");

feed.setLink("http://www.jikedaquan.com");

super.buildFeedMetadata(model, feed, request);

}

@Override

protected List buildFeedItems(Map model,

HttpServletRequest request, HttpServletResponse response) throws Exception {

List listContent = (List) model.get("feedContent");

List items = new ArrayList(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 items = new ArrayList();

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 配置如下所示 -

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

上面的代码中创建了一个RSS feed POJO RSSMessage和一个RSS Message Viewer,它扩展了AbstractRssFeedView并覆盖了它的方法。 在RSSController中,生成了一个示例RSS Feed。

完成创建源和配置文件后,发布应用程序到Tomcat服务器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.cnfilm.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.cnfilm.web.film.Film; import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Description; import com.sun.syndication.feed.rss.Item; import com.sun.syndication.io.FeedException; import com.sun.syndication.io.WireFeedOutput; /** * 文件名:RssUtils.java 网站RSS生成 * 版本信息:V1.0 * 日期:2013-06-18 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class RssUtils { public static String getRssString(List filmList,HashMap map){ Channel channel = new Channel("rss_2.0"); channel.setTitle(map.get("title")); channel.setDescription(map.get("description")); channel.setLink("http://www.bdvcd.com/"); channel.setEncoding("UTF-8"); /**这个list对应rss中的item列表**/ List items = new ArrayList(); /**新建Item对象,对应rss中的**/ Item item = null; for(Film film:filmList){ item = new Item(); item.setAuthor(film.getStr("starring")); item.setLink("http://www.bdvcd.com/"+film.getStr("curl")+"/"+film.getStr("url")+".html"); item.setPubDate(DateUtils.parse(film.getStr("addtime"))); item.setTitle(film.getStr("fname")); Description description = new Description(); description.setValue(film.getStr("content")); item.setDescription(description); items.add(item); } channel.setItems(items); /**用WireFeedOutput对象输出rss文本**/ WireFeedOutput out = new WireFeedOutput(); String rssString = ""; try { rssString = out.outputString(channel); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (FeedException e) { e.printStackTrace(); } return rssString; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值