Rss,Atom,基本上是Web2.0的基本特征,今天终于有时间决定实现一下。对于动态的Rss,Atom其实就是选取最新的内容,迭迨一下生成一个固定格式的xml文件就可以了。其实也很简单。
我用webwork2,用freemarker做模版,在网上找了一个标准的Rss2.0格式就生成我的模版文件rss.ftl
Thu, 6 Apr 2006 16:01:36 GMT
做好了这些,配置一下action
因为返回结果的contentType是text/html.而不是xml,这样是不行的。看了一下webwork源码。原来可以这样简单的解决。改成这样配置就可以了。
简单吧。其实你还可以把这个contentType改成其他类型的。比如excle的。这样用户执行就可以得到一个xls文件。哈哈。
我用webwork2,用freemarker做模版,在网上找了一个标准的Rss2.0格式就生成我的模版文件rss.ftl
<?
xml version="1.0" encoding="UTF-8"
?>
< rss version ="2.0" >
< channel >
< title > <![CDATA[ 我的小站 ]]> </ title >
< link > http://leaf.jdk.cn/ </ link >
< description > <![CDATA[ 泡出好心情,泡出好技术 ]]> </ description >
< language > zh-cn </ language >
< copyright > <![CDATA[ Copyright 2006 PaoPao v4.0 ]]> </ copyright >
< webMaster > <![CDATA[ rory.cn@gmail.com (Rory Ye) ]]> </ webMaster >
< generator > PaoPao v4.0 </ generator >
< image >
< title > 我的小站 </ title >
< url > http://leaf.jdk.cn/logo.gif </ url >
< link > http://leaf.jdk.cn </ link >
< description > 莫多泡泡2006 </ description >
</ image >
< #list blogs as blog >
< item >
< link > http://leaf.jdk.cn/entry.action?entryId=${blog.id} </ link >
< title > <![CDATA[ ${blog.title} ]]> </ title >
< author > ${blog.authorMail}(${blog.author}) </ author >
< category > <![CDATA[ ${blog.categories} ]]> </ category >
< pubDate > ${blog.pubDate} </ pubDate >
< guid > http://leaf.jdk.cn/entry.action?id=${blog.id} </ guid >
< description > <![CDATA[ ${blog.content} ]]> </ description >
</ item >
</ #list >
</ channel >
</ rss >
我在rss.action里面去取blogs就可以了。
< rss version ="2.0" >
< channel >
< title > <![CDATA[ 我的小站 ]]> </ title >
< link > http://leaf.jdk.cn/ </ link >
< description > <![CDATA[ 泡出好心情,泡出好技术 ]]> </ description >
< language > zh-cn </ language >
< copyright > <![CDATA[ Copyright 2006 PaoPao v4.0 ]]> </ copyright >
< webMaster > <![CDATA[ rory.cn@gmail.com (Rory Ye) ]]> </ webMaster >
< generator > PaoPao v4.0 </ generator >
< image >
< title > 我的小站 </ title >
< url > http://leaf.jdk.cn/logo.gif </ url >
< link > http://leaf.jdk.cn </ link >
< description > 莫多泡泡2006 </ description >
</ image >
< #list blogs as blog >
< item >
< link > http://leaf.jdk.cn/entry.action?entryId=${blog.id} </ link >
< title > <![CDATA[ ${blog.title} ]]> </ title >
< author > ${blog.authorMail}(${blog.author}) </ author >
< category > <![CDATA[ ${blog.categories} ]]> </ category >
< pubDate > ${blog.pubDate} </ pubDate >
< guid > http://leaf.jdk.cn/entry.action?id=${blog.id} </ guid >
< description > <![CDATA[ ${blog.content} ]]> </ description >
</ item >
</ #list >
</ channel >
</ rss >
public
String execute()
throws
Exception {
PaginationSupport ps = blogManager.getBlogsByPage( 0 );
blogs = new ArrayList();
for (Iterator ite = ps.getItems().iterator(); ite.hasNext();){
Blog blog = (Blog)ite.next();
WrapRssBlog wrapBlog = new WrapRssBlog();
wrapBlog.setId(blog.getId());
wrapBlog.setAuthor(blog.getAuthor().getNickname());
wrapBlog.setAuthorMail(blog.getAuthor().getMail());
wrapBlog.setTitle(blog.getTitle());
wrapBlog.setContent(StringUtil.extractText(blog.getContent()));
wrapBlog.setPubDate(DateUtil.formatRssDate(blog.getPostTime()));
StringBuffer sb = new StringBuffer();
for (Iterator ite2 = blog.getCategories().iterator(); ite2.hasNext();){
Category cate = (Category) ite2.next();
sb.append( " " )
.append(cate.getName());
}
wrapBlog.setCategories(sb.toString());
blogs.add(wrapBlog);
}
return super .execute();
}
这里把blog包装了一下,主要是处理了一下日期,内容,还有把分类拼成字符串。关于日期的处理,参考了飞云小侠写的rss日期的研究。实现代码如下。
PaginationSupport ps = blogManager.getBlogsByPage( 0 );
blogs = new ArrayList();
for (Iterator ite = ps.getItems().iterator(); ite.hasNext();){
Blog blog = (Blog)ite.next();
WrapRssBlog wrapBlog = new WrapRssBlog();
wrapBlog.setId(blog.getId());
wrapBlog.setAuthor(blog.getAuthor().getNickname());
wrapBlog.setAuthorMail(blog.getAuthor().getMail());
wrapBlog.setTitle(blog.getTitle());
wrapBlog.setContent(StringUtil.extractText(blog.getContent()));
wrapBlog.setPubDate(DateUtil.formatRssDate(blog.getPostTime()));
StringBuffer sb = new StringBuffer();
for (Iterator ite2 = blog.getCategories().iterator(); ite2.hasNext();){
Category cate = (Category) ite2.next();
sb.append( " " )
.append(cate.getName());
}
wrapBlog.setCategories(sb.toString());
blogs.add(wrapBlog);
}
return super .execute();
}
public
static
String formatRssDate(Date date){
SimpleDateFormat sdf = new SimpleDateFormat( " EEE, d MMM yyyy HH:mm:ss z " ,Locale.US);
// TODO TimeZone
SimpleTimeZone zone = new SimpleTimeZone( 8 , " GMT " );
sdf.setTimeZone(zone);
return sdf.format(date);
}
这样就得到了这样的日期
SimpleDateFormat sdf = new SimpleDateFormat( " EEE, d MMM yyyy HH:mm:ss z " ,Locale.US);
// TODO TimeZone
SimpleTimeZone zone = new SimpleTimeZone( 8 , " GMT " );
sdf.setTimeZone(zone);
return sdf.format(date);
}
Thu, 6 Apr 2006 16:01:36 GMT
做好了这些,配置一下action
<
action
name
="rss"
class
="cn.jdk.leaf.action.RssAction"
>
< result name ="success" type ="freemarker" > /WEB-INF/pages/rss.ftl </ result >
</ action >
不过像这样配置的话,你访问
http://xxx.xxx.com/rss.action看到的不是一个xml< result name ="success" type ="freemarker" > /WEB-INF/pages/rss.ftl </ result >
</ action >
因为返回结果的contentType是text/html.而不是xml,这样是不行的。看了一下webwork源码。原来可以这样简单的解决。改成这样配置就可以了。
<
action
name
="rss"
class
="cn.jdk.leaf.action.RssAction"
>
< result name ="success" type ="freemarker" >
< param name ="location" > /WEB-INF/pages/rss.ftl </ param >
< param name ="contentType" > application/xml </ param >
</ result >
</ action >
< result name ="success" type ="freemarker" >
< param name ="location" > /WEB-INF/pages/rss.ftl </ param >
< param name ="contentType" > application/xml </ param >
</ result >
</ action >
简单吧。其实你还可以把这个contentType改成其他类型的。比如excle的。这样用户执行就可以得到一个xls文件。哈哈。