Rory's Blog
Happy study,Happy work,Happy life
posts - 22,  comments - 46,  trackbacks - 0
Rss,Atom,基本上是Web2.0的基本特征,今天终于有时间决定实现一下。对于动态的Rss,Atom其实就是选取最新的内容,迭迨一下生成一个固定格式的xml文件就可以了。其实也很简单。
       我用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就可以了。
    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日期的研究。实现代码如下。
    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);
    }
这样就得到了这样的日期
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
因为返回结果的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>

简单吧。其实你还可以把这个contentType改成其他类型的。比如excle的。这样用户执行就可以得到一个xls文件。哈哈。
posted on 2006-04-10 22:40 莫多 阅读(1307) 评论(2)  编辑  收藏 所属分类: Webwork

FeedBack:
# re: webwork+freemarker轻松实现Rss,Atom
2007-03-30 16:27 | 晓菲

Rory老师:您好!

我是电子工业出版社博文视点的编辑晓菲,有幸在网上拜读到您的大作,得知您比较喜欢RSS与Atom方面的知识。博文视点现在正在寻找《RSS and Atom in Action》一书的译者,不知您是否有兴趣。

如果您愿意与博文视点关于此书进行更进一步的沟通,敬请您与我联系,我的邮箱地址是:xiaofei@broadview.com.cn

Thanks  回复  更多评论
  
# re: webwork+freemarker轻松实现Rss,Atom
2008-02-23 11:24 | nika
This is website.a lot of guys can service for you , most skilled levelers and The price of power leveling. Give all customers the best and cheap price. The best and securest way to power level your character to your desired high level fast. the website already have done near a number of orders for wow power leveling and offers professional powerleveling service on most popular MMOGs.   回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2008年2月>
272829303112
3456789
10111213141516
17181920212223
2425262728291
2345678

常用链接

留言簿(1)

随笔分类(27)

随笔档案(22)

Friends

搜索

  •  

积分与排名

  • 积分 - 61416
  • 排名 - 847

最新评论

阅读排行榜

评论排行榜