闲来无事写了一个java生成RSS的小组件,本人没有参考过其他的开源的组件,不过个人认为这种输出方式比较易用,
主要是用到了java的反射机制.
大概功能:
在你的PO里面定义某些annotaions 如:Author,Description,Guid,Link,Param,PubDate,Title
和你需要生成RSS里面的标签是同名的
当你从数据库取值返回List之后 值就已经进去了..
整个调用过程如下:
特别要说明的是item.getParams()
在RSS里面有一个link标签 作用是可以根据这个Link标签跳到具体的页面.
但这个时候需要得到对象的ID 或者其他的信息,才能跳过去.
所以你只需要在PO里面使用 params 的annotaion 来定义你的字段就可以获取到值
如
@Param
private Long id;
//gets sets
.....
item.getParams("id");就能取到
或
@Param("p_id")
private Long id;
//gets sets
item.getParams("p_id");
也能取到
.....
具体的请看源码..
查看效果 http://www.uuke.cn/place/findTypePlace---1---rss.html
主要是用到了java的反射机制.
大概功能:
在你的PO里面定义某些annotaions 如:Author,Description,Guid,Link,Param,PubDate,Title
和你需要生成RSS里面的标签是同名的
当你从数据库取值返回List之后 值就已经进去了..
整个调用过程如下:
//这个list是从数据库取的
List placeList =placeService.getPlaceList(cecondition, pager, request);
//这个类是创建RSS模板以及生成XML格式的类
RssTemplet rssTemplet=RssTemplet.newInstance();
Channel channel;
try {
//生成具体的RSS模板
channel = rssTemplet.createRssTemplet(placeList);
//设置标题头信息
channel.setTitle("uuke 场所");
channel.setDescription("uuke 吃喝玩乐场所列表");
channel.setLink("http://www.uuke.cn");
channel.setGenerator("长沙最大的吃喝玩乐社区");
//具体的列表项
Set items=channel.getItems();
Iterator iterator=items.iterator();
while(iterator.hasNext()){
Item item=(Item)iterator.next();
item.setLink("http://www.uuke.cn/place/go_"+ item.getParams("placeId") +".html");
item.setDescription(
"<div id=\"box\">"+
"<img src=\"http://www.uuke.cn/"+ item.getParams("isimage") +"_2.gif\" />"+
"<p>地址:"+ item.getParams("placeAddr") +"</p>"+
"<p>电话:"+ item.getParams("homePhone") +"</p>"+
"<p>平均消费:"+ item.getParams("avgMonly") +"元/人</p>"+
"<p>"+ item.getDescription() +"</p>"+
"</div>"
);
item.setAuthor("uuke.cn");
item.setGuid(item.getLink());
}
sb=rssTemplet.createRss(channel);
} catch (Exception e) {
e.printStackTrace();
}
特别要说明的是item.getParams()
在RSS里面有一个link标签 作用是可以根据这个Link标签跳到具体的页面.
但这个时候需要得到对象的ID 或者其他的信息,才能跳过去.
所以你只需要在PO里面使用 params 的annotaion 来定义你的字段就可以获取到值
如
@Param
private Long id;
//gets sets
.....
item.getParams("id");就能取到
或
@Param("p_id")
private Long id;
//gets sets
item.getParams("p_id");
也能取到
.....
具体的请看源码..
查看效果 http://www.uuke.cn/place/findTypePlace---1---rss.html
本文介绍了一种使用Java生成RSS的方法,通过定义特定的注解,能够轻松地将数据库中的数据转换为RSS格式,并且提供了丰富的自定义选项。
423

被折叠的 条评论
为什么被折叠?



