呵呵,其实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 代码
- 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 = "";
-
-
-
- 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");
- 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;
- }
-
- }
- return "PutOk";
- }
然后在页面上生成如下:(参考了javaeyeRSS订阅)
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订阅此博客" />
呵呵,是不是很简单.