RSS也叫聚合RSS是在線共享內容的一種簡易方式(也叫聚合內容,Really Simple Syndication)。一般在時效性比較強的內容上使用RSS訂閱能更快速獲取信息,網站提供RSS輸出,有利於讓用戶獲取網站內容的最新更新。
rss好處不用多說了,咱們直接看一下在使用Struts2下如何實現。
在struts配置文件中設置result類型爲 plainText
html
xhtml 代碼
- <result type="plainText"></result>
action類裏使用writer直接輸出rss標準的xml ,這裏我使用了第三方的開源工具包RSSLibJ幫我快速輸出標準格式的xml。java
java 代碼
- Channel channel = new Channel();
- channel.setDescription("協做,分享,攜手共建企業業務組件的社區平臺");
- channel.setLink("http://po-soft.com/bcs");
- channel.setTitle("組件文章-OECP社區");
- channel.addItem("http://po-soft.com/bc/oecp/wiki1.0/section/5a015gppnang00b0",
- "一、如何註冊社區賬號?點擊社區右上方的「註冊」進行註冊。 ...",
- "註冊登陸");
- channel.addItem("http://po-soft.com/bc/oecp-gl/wiki0.1/section/47615g5a85nu028e",
- " i. 功能設計 ¡¤ 標準會計報表包括:現金流量表、利潤表、資產負債表..."
- , "標準會計報表");
- ServletActionContext.getResponse().setContentType("xml+rss;charset=utf-8");
- ServletActionContext.getResponse().getWriter().write(channel.getFeed("2.0"));
輸出後的樣式:
xhtml 代碼
- <?xml version='1.0' encoding='UTF-8'?>
- <rss version='2.00'>
- <channel>
- <title>組件文章-OECP社區</title>
- <link>http://po-soft.com/bcs</link>
- <description>協做,分享,攜手共建企業業務組件的社區平臺</description>
- <docs>http://backend.userland.com/rss</docs>
- <generator>RSSLibJ (http://rsslibj.sourceforge.net/)</generator>
- <item>
- <title>註冊登陸</title>
- <link>http://po-soft.com/bc/oecp/wiki1.0/section/5a015gppnang00b0</link>
- <description>一、如何註冊社區賬號?點擊社區右上方的「註冊」進行註冊。 ...</description>
- </item>
- <item>
- <title>標準會計報表</title>
- <link>http://po-soft.com/bc/oecp-gl/wiki0.1/section/47615g5a85nu028e</link>
- <description> i. 功能設計 ¡¤ 標準會計報表包括:現金流量表、利潤表、資產負債表...</description>
- </item>
- </channel>
- </rss>
jsp頁面中設置rss地址。點亮瀏覽器rss訂閱的圖標
瀏覽器
xhtml 代碼
- <link href="/blogs/rss" rel="alternate" title="組件文章-OECP社區" type="application/rss+xml" />
點亮rss圖標後用戶就能夠方便的使用ie或者其餘閱讀器進行訂閱。固然您也能夠設置多個rss。
![點亮rss圖標](http://static.javashuo.com/static/loading.gif)
經過ie8打開就能夠方便的訂閱本網站了
![IE8自帶的rss訂閱器](http://static.javashuo.com/static/loading.gif)
到此rss的輸出就完成了,你也能夠經過支持Rss訂閱瀏覽器或者其餘工具(好比FeedDemon或者Goolge Reader等)甚至是手機或者pda等設備方便的進行訂閱了。
本文使用到的jar包能夠在OECP社區 下載app