我用webwork2,用freemarker做模版,在網上找了一個標準的Rss2.0格式就生成我的模版文件rss.ftl
<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[[email protected] (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>
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();
}
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
<result name="success" type="freemarker">/WEB-INF/pages/rss.ftl</result>
</action>
因爲返回結果的contentType是text/html.而不是xml,這樣是不行的。看了一下webwork源碼。原來可以這樣簡單的解決。改成這樣配置就可以了。
<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文件。哈哈。