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
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
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14734416/viewspace-442253/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/14734416/viewspace-442253/