posts - 431,  comments - 344,  trackbacks - 0
          公告
           Don't Repeat Yourself
          座右銘:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
          公告本博客在此聲明部分文章為轉摘,只做資料收集使用。


          微信: szhourui
          QQ:109450684
          Email
          lsi.zhourui@gmail.com
          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          留言簿(15)

          隨筆分類(1019)

          文章分類(3)

          文章檔案(21)

          收藏夾

          Link

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 863035
          • 排名 - 44

          最新評論

          閱讀排行榜

          Grails本身好像并沒有產生rss的插件。如果要用grails產生Rss,可以使用Rome。方法大概如下:

          下載需要的庫文件

          https://rome.dev.java.net/下載Rome,之后把rome-xxx.jar放到你的grails項目的lib目錄下。這里xxx是版本號。比如我的是rome-1.0RC1.jar

          再到http://www.jdom.org/下載JDom。之后同樣是把jdom.jar放到lib目錄下。

          書寫代碼

          創建一個controller,當然你也可以在你已經有的controller里面增加相應方法。這里我們創建一個叫做FeedController的類。

           1 import com.sun.syndication.feed.synd.*;   
           2 import com.sun.syndication.io.SyndFeedOutput;   
           3                
           4 class FeedController {   
           5        
           6     def supportedFormats = [ "rss_0.90""rss_0.91""rss_0.92""rss_0.93""rss_0.94""rss_1.0""rss_2.0""atom_0.3"]   
           7   
           8                   
           9                                 
          10        
          11     def rss = {   
          12                
          13             render(text: getFeed("rss_1.0"), contentType:"text/xml", encoding:"UTF-8")   
          14        
          15     }   
          16        
          17     def atom = {   
          18                
          19             render(text: getFeed("atom_1.0"), contentType:"text/xml", encoding:"UTF-8")   
          20                        
          21     }   
          22   
          23     // or specify your own feed type   
          24     def all = {   
          25             def format = params.id   
          26             if (supportedFormats.contains(format)) {   
          27                 render(text: getFeed(format), contentType:"text/xml", encoding:"UTF-8")   
          28             } else {   
          29                 response.sendError(response.SC_FORBIDDEN);   
          30             }                  
          31     }   
          32        
          33        
          34     def getFeed(feedType) {   
          35            
          36         def items = Post.list(max: 5, sort: "created", order: "desc")   
          37                
          38         def entries = []   
          39         items.each { item ->   
          40             def desc = new SyndContentImpl(type: "text/plain", value: item.description);   
          41             def entry = new SyndEntryImpl(title: item.name + " - " + item.summary,    
          42                     link: 'http://www.ondev.net/item/show/' + item.name,   
          43                     publishedDate: item.created, description: desc);   
          44             entries.add(entry);   
          45   
          46         }   
          47         SyndFeed feed = new SyndFeedImpl(feedType: feedType, title: '標題',   
          48                 link: 'http://www.ondev.net', description: '說明性文字',   
          49                 entries: entries);   
          50            
          51         StringWriter writer = new StringWriter();   
          52         SyndFeedOutput output = new SyndFeedOutput();   
          53         output.output(feed,writer);   
          54         writer.close();   
          55            
          56         return writer.toString();   
          57   
          58            
          59     }   
          60        
          61 }  
          62 

          之后訪問相應的頁面,比如說http://www.ondev.net/feed/rss就可以了

          原貼地址: http://www.ondev.net/story/show/75

          posted on 2008-09-07 22:53 周銳 閱讀(293) 評論(0)  編輯  收藏 所屬分類: Groovy&Grails
          主站蜘蛛池模板: 南江县| 陆丰市| 峡江县| 高唐县| 鹿邑县| 额济纳旗| 清水河县| 宁阳县| 水城县| 铁岭县| 鹿邑县| 嘉黎县| 明星| 东宁县| 濉溪县| 东乡县| 连城县| 长宁区| 宜兴市| 东乡| 彰武县| 呼伦贝尔市| 九江市| 新竹县| 普兰县| 崇仁县| 香河县| 莱芜市| 三河市| 舞阳县| 襄垣县| 防城港市| 通河县| 九龙坡区| 绥芬河市| 汶上县| 千阳县| 类乌齐县| 武安市| 龙南县| 淮北市|