2007-08-21
给你的网站增加RSS功能
关键字: RSS rome ZHAXIA GOOGLE
呵呵,其实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下面.
开始写代码了>
然后在页面上生成如下:(参考了javaeyeRSS订阅)
呵呵,是不是很简单.
我经过反复的筛选和使用,认为使用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 代码
- public ActionForward RssPut(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- /**
- * 产生二级分类
- */
- String Guangao = "<br>";
- DateFormat DATE_PARSER = new SimpleDateFormat("yyyy-MM-dd");
- // 生成静态页面
- ServletContext servletContext = servlet.getServletContext();
- String realPath = servletContext.getRealPath("/");
- List ProductType1 = productDAO.getProductTreeLayer("0");
- List ProductType2 = productDAO.getProductTreeLayer("1");
- List ProductType3 = productDAO.getProductTreeLayer("2");
- /**
- * 生成首页
- */
- String path = realPath + "AntBeeRss/";
- String fileName = "";
- /**
- * 从这里开始循环,产品名称的ID来
- */
- for (int i = 0; i < ProductType2.size(); i++) {
- Product product = (Product) ProductType2.get(i);
- File newsDir = new File(path);
- if (newsDir.exists()) {
- fileName = path + "/AntBeeRss_"
- + product.getTypeDir().toString() + ".xml";
- } else {
- newsDir.mkdirs();
- fileName = path + "/AntBeeRss_"
- + product.getTypeDir().toString() + ".xml";
- }
- SyndFeed feed = new SyndFeedImpl();
- feed.setFeedType("rss_2.0");
- feed.setTitle(product.getSonName() + "大全,报价,功能描述");
- feed.setAuthor("AntBee Tech Corp.");
- feed.setCopyright("Copyright 2007-2008, AntBee Tech Corp.");
- feed.setLink("http://www.1718zx.cn");
- feed.setDescription("AntBee Tech Corp.");
- feed.setEncoding("GBK");//使用GBK,否则zhuaxia和google都不认识。
- feed.setLanguage("GBK");
- List entries = new ArrayList();
- List product2 = productInfoDAO.getProductinfo2(product.getId()
- .toString());
- if (product2 != null) {
- for (int j = 0; j < product2.size(); j++) {
- Productinfo info = (Productinfo) product2.get(j);
- SyndEntry entry;
- SyndContent description;
- entry = new SyndEntryImpl();
- String manufacturer = (info.getManufacturer() == null) ? ""
- : info.getManufacturer().getManufacturerName();
- entry.setTitle(info.getProductName() + ",型号:"
- + info.getProductSpec() + ",厂商:" + manufacturer);
- entry.setLink("http://www.1718zx.cn/Product/"
- + info.getProductSpec() + ".html");
- entry.setPublishedDate(new Date());
- description = new SyndContentImpl();
- description.setType("text/plain");
- description.setValue(info.getProductDesc() + Guangao);
- entry.setDescription(description);
- entries.add(entry);
- }
- }
- try{
- feed.setEntries(entries);
- Writer writer = new FileWriter(fileName);
- SyndFeedOutput output = new SyndFeedOutput();
- output.output(feed, writer);
- writer.close();
- }catch(Exception e){
- continue;//在很多时候,有些txt文本当中不符合xml的一些规范,而我把它省略过去.
- }
- }
- return "PutOk";
- }
js 代码
- <a href="../AntBeeRss/AntBeeRss_${typeDir?if_exists}.xml"><img src="../AntBeeCMS/images/rss2.gif" alt="RSS订阅此博客" /></a>
- <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>
- <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订阅此博客" />
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 116529 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
我的相册
mvn-project.jpg
共 37 张
共 37 张
最近加入圈子
最新评论
-
Netbeans VS Myeclipse和 ...
冷...Netbeans VS Myeclipse和自动档汽车 vs 手动档汽车 ...
-- by lordhong -
Netbeans VS Myeclipse和 ...
我也喜欢EMACS,JDE挺好用的,就是不知道怎么调试WEB程序
-- by lululau -
使用openfire+jwchat来构 ...
楼上只说了在线消息的发送代码,我想知道:离线消息是怎么保存的呢?
-- by jiahx -
使用openfire+jwchat来构 ...
ajax都存在一个跨域的问题,看apache是用url rewrite解决的,不 ...
-- by ayeah -
如何利用OSWorkflow的func ...
function和condition里的actionId可以通过transien ...
-- by fantasy






评论排行榜