使用Rome实现网站RSS发布的简单步骤

<% @ page language = " java "  contentType = " text/html; charset=UTF-8 "  pageEncoding = " UTF-8 " %>
<% @include file = " package.inc.jsp " %>
<% @page  import = " org.jdom.*,com.sun.syndication.feed.synd.*,com.sun.syndication.io.SyndFeedOutput " %>
<%
  
int  maxLength  =   500 ;
  out.clearBuffer();
  SyndFeed feed 
=   new  SyndFeedImpl();
  feed.setFeedType(
" rss_2.0 " );
  feed.setTitle(
" Java世纪网,Enjoy Java, Enjoy Everyday " );
  feed.setLink(
" http://www.java2000.net " );
  feed.setDescription(
" Java世纪网的最新的50个帖子列表 " );
  feed.setEncoding(
" UTF-8 " );  
 
/* 
  Document doc = feed.outputJDom(feed.createWireFeed());
  //create the XSL processing instruction
  Map xsl = new HashMap();
  xsl.put("href", "
http://feeds.feedburner.com/~d/styles/rss2full.xsl");
  xsl.put("type", "text/xsl");
  xsl.put("media", "screen");
  ProcessingInstruction pXsl = new ProcessingInstruction("xml-stylesheet", xsl);
  doc.addContent(0, pXsl);
  //create the CSS processing instruction
  Map css = new HashMap();
  css.put("href", "
http://feeds.feedburner.com/~d/styles/itemcontent.css");
  css.put("type", "text/css");
  css.put("media", "screen");
  ProcessingInstruction pCss = new ProcessingInstruction("xml-stylesheet", css);  
  doc.addContent(1, pCss);  
  
*/

  
  List
< SyndEntry >  entries  =   new  ArrayList < SyndEntry > ();
  SyndEntry entry;
  SyndContent description;
  PostService ps 
=  (PostService) Factory.getBean( " PostService " );
  Collection
< Post >  pList  =  ps.findAll( - 1 0 50 );
  
for  (Post p : pList)  {
    entry 
= new SyndEntryImpl();
    entry.setTitle(p.getUser().getUsername()
+"("+p.getUser().getNickName()+")"+(p.getIdParent()==0?"发表了":"回复了")+":"+p.getSubject());
    entry.setLink(
"http://www.java2000.net/viewthread.jsp?tid=" + p.getId() + "");
    entry.setPublishedDate(p.getDatetime());
    description 
= new SyndContentImpl();
    description.setType(
"text/html");
    
if(p.getContent().length()>maxLength){
      description.setValue(p.getContent().substring(
0,maxLength)+"<br/><br/>【内容太长,只显示了前"+maxLength+"字,更多内容请看全文】");
    }
else{
      description.setValue(p.getContent());
    }

    entry.setDescription(description);
    entries.add(entry);
  }

  feed.setEntries(entries);
  
try   {
    SyndFeedOutput output 
= new SyndFeedOutput();
    out.print(output.outputString(feed));
  }
  catch  (Exception ex)  {
    ex.printStackTrace();
  }

%>

 

代码很简单,中间屏蔽的部分是我还没有测试的,自定义xsl的部分。

其他的都已经正常运行,测试地址 http://www.java2000.net/rss.jsp

 







<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值