给你的网站增加RSS功能

     呵呵,其实RSS很简单.因为他的代码量实在是不大,而且很容易理解,
    我经过反复的筛选和使用,认为使用rome是目前比较简捷而且高效的生成RSS的好工具.而我以前是使用freemarker来生成rss,但是生成的rss不标准,本文主要是实践为主,列举出实际代码.
    首先下载最新的rome版本,[url=https://rome.dev.java.net/source/browse/*checkout*/rome/www/dist//rome-0.9.zip]rome0.9下载地址[/url]
    然后把rome-0.9.jar放在你的web-inf/lib下面.
    开始写代码了>
   
java 代码
 
  1. public ActionForward RssPut(ActionMapping mapping, ActionForm form,  
  2.             HttpServletRequest request, HttpServletResponse response)  
  3.             throws Exception {  
  4.         /** 
  5.          * 产生二级分类 
  6.          */       
  7.         String Guangao = "<br>";          
  8.         DateFormat DATE_PARSER = new SimpleDateFormat("yyyy-MM-dd");  
  9.         // 生成静态页面  
  10.         ServletContext servletContext = servlet.getServletContext();  
  11.         String realPath = servletContext.getRealPath("/");  
  12.         List ProductType1 = productDAO.getProductTreeLayer("0");  
  13.         List ProductType2 = productDAO.getProductTreeLayer("1");  
  14.         List ProductType3 = productDAO.getProductTreeLayer("2");  
  15.   
  16.         /** 
  17.          * 生成首页 
  18.          */  
  19.         String path = realPath + "AntBeeRss/";  
  20.   
  21.         String fileName = "";  
  22.         /** 
  23.          * 从这里开始循环,产品名称的ID来 
  24.          */  
  25.         for (int i = 0; i < ProductType2.size(); i++) {  
  26.             Product product = (Product) ProductType2.get(i);              
  27.             File newsDir = new File(path);  
  28.             if (newsDir.exists()) {  
  29.                 fileName = path + "/AntBeeRss_"  
  30.                         + product.getTypeDir().toString() + ".xml";  
  31.             } else {  
  32.                 newsDir.mkdirs();  
  33.                 fileName = path + "/AntBeeRss_"  
  34.                         + product.getTypeDir().toString() + ".xml";  
  35.             }  
  36.             SyndFeed feed = new SyndFeedImpl();  
  37.             feed.setFeedType("rss_2.0");  
  38.             feed.setTitle(product.getSonName() + "大全,报价,功能描述");  
  39.             feed.setAuthor("AntBee Tech Corp.");  
  40.             feed.setCopyright("Copyright 2007-2008, AntBee Tech Corp.");  
  41.             feed.setLink("http://www.1718zx.cn");  
  42.             feed.setDescription("AntBee Tech Corp.");  
  43.             feed.setEncoding("GBK");//使用GBK,否则zhuaxia和google都不认识。  
  44.             feed.setLanguage("GBK");  
  45.             List entries = new ArrayList();  
  46.   
  47.             List product2 = productInfoDAO.getProductinfo2(product.getId()  
  48.                     .toString());  
  49.             if (product2 != null) {  
  50.                 for (int j = 0; j < product2.size(); j++) {  
  51.                     Productinfo info = (Productinfo) product2.get(j);  
  52.                     SyndEntry entry;  
  53.                     SyndContent description;  
  54.                     entry = new SyndEntryImpl();  
  55.                     String manufacturer = (info.getManufacturer() == null) ? ""  
  56.                             : info.getManufacturer().getManufacturerName();  
  57.                     entry.setTitle(info.getProductName() + ",型号:"  
  58.                             + info.getProductSpec() + ",厂商:" + manufacturer);  
  59.                     entry.setLink("http://www.1718zx.cn/Product/"  
  60.                             + info.getProductSpec() + ".html");  
  61.                     entry.setPublishedDate(new Date());  
  62.                     description = new SyndContentImpl();  
  63.                     description.setType("text/plain");  
  64.                     description.setValue(info.getProductDesc() + Guangao);                    
  65.                     entry.setDescription(description);  
  66.                     entries.add(entry);  
  67.                 }  
  68.             }  
  69.             try{  
  70.                 feed.setEntries(entries);  
  71.                 Writer writer = new FileWriter(fileName);  
  72.                 SyndFeedOutput output = new SyndFeedOutput();  
  73.                 output.output(feed, writer);  
  74.                 writer.close();  
  75.             }catch(Exception e){  
  76.                 continue;//在很多时候,有些txt文本当中不符合xml的一些规范,而我把它省略过去.  
  77.             }  
  78.               
  79.         }  
  80.         return "PutOk";  
  81.     }  
然后在页面上生成如下:(参考了javaeyeRSS订阅)
js 代码
 
  1. <a href="../AntBeeRss/AntBeeRss_${typeDir?if_exists}.xml"><img src="../AntBeeCMS/images/rss2.gif" alt="RSS订阅此博客" /></a>  
  2.    <a href="http://www.zhuaxia.com/add_channel.php?url=http://www.1718zx.cn/AntBeeRss/AntBeeRss_${typeDir?if_exists}.xml"><img src="../AntBeeCMS/images/rss_zhuaxia.gif" alt="用抓虾订阅此博客" /></a>       
  3.    <a href="http://fusion.google.com/add?feedurl=http://www.1718zx.cn/AntBeeRss/AntBeeRss_${typeDir?if_exists}.xml"><img src="../AntBeeCMS/images/rss_google.gif" alt="用google订阅此博客" />   
呵呵,是不是很简单.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值