Scud (Flying Cloud Xia) http://www.jscud.com reprinted please indicate the author/source
Keywords: RSS, freemarker, RSS. XML, webwork2
RSS is widely used on the Internet, and RSS support is added to various websites. I recently studied it and added RSS subscriptions to my articles.
There are several versions of RSS currently used, which is very confusing. The following uses rss2.0 as an example.
There is an rsslibj library on the network, which is used to generate RSS support files, but it has not been updated for a long time. It is generated using XML. the example of this article does not need to any XML parser, but of course you need to know the format of the last generated XML file, about RSS specifications, you can browse the http://blogs.law.harvard.edu/tech/rss.
When planning to generate RSS files, I searched for Jira and confluence programs and found that they were displayed using templates and JSP dynamic pages. so I also came up with two methods:
1. Use freemarker to generate static files, which is suitable for content that is not updated frequently.
2. Dynamic Display with JSP, suitable for high update frequency and a wide range of content.
Take this site News for example, the news information class reference http://www.jscud.com/srun/news/viewhtml/3_2005_8/76.htm, here not listed.
(1) freemarker method.
According to the RSS specification, the template is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> & Lt; RSS version = "2.0" & gt; <Channel> <Title> jscud develop </title> <Link> http://www.jscud.com/</link> <Language> ZH-CN </language> <Description> jscud develop by Scud </description> <Webmaster> xxx@21cn.com (SCUD) </Webmaster> <Lastbuilddate >$ {rssutil. formatrssdate (now)} </lastbuilddate>
<# List newslist as onenews> <Item> <Title >$ {onenews. Title? XML} </title> <Link> Links </link> <Pubdate >$ {rssutil. formatrssdate (onenews. addtime)} </pubdate> <Description> <! [CDATA [ $ {Rssutil. formatrsscdata (onenews. showcontent )} ]> </Description> </Item> </# List> </Channel> </RSS> |
The URL and website name can be modified according to your actual situation.
Every time I retrieve the latest 20 articles to generate RSS, but there are a lot of content and the generated RSS file is large. I can see that some websites only include the description of the article abstract, in this way, the file is much smaller. in short, it is designed according to your own needs.
The functions of the rssutil function library used are as follows (for date functions, refer to the previous article ):
/** * Replace]> with] & gt; * @ Param content * @ Return formatted content */ Public static string formatrsscdata (string content) { String result = stringfunc. Replace (content, "//]>", "] & gt ;");
Return result; }
/** * Formatted as a string required by XML * @ Param field content * @ Return formatted string */ Public static string formatstring2xml (string field) { Return stringfunc. str2textxml (field ); }
Public static string getnowdatetime () { Return formatrssdate (datetime. getnowtimestamp ()); } |
The code for generating static files using freemarker is as follows:
Private configuration freemarker_cfg = NULL; Protected configuration getfreemarkercfg () { If (null = freemarker_cfg) { // Initialize the freemarker configuration; //-Create a configuration instance Freemarker_cfg = new configuration (); Freemarker_cfg.setclassfortemplateloading (this. getclass (), "/htmlskin "); Freemarker_cfg.setdefaultencoding ("GBK "); } Return freemarker_cfg; } Public Boolean genefilebyfreemarker (string templatefilename, map propmap, string filepath, String filename, string encode) { Try { Template T = getfreemarkercfg (). gettemplate (templatefilename ); File afile = new file (filepath + "/" + filename ); Writer out = new bufferedwriter (New outputstreamwriter (New fileoutputstream (afile ), Encode )); Propmap. Put ("baseurl", propset. getstringprop ("url. Root ")); T. Process (propmap, out ); } Catch (templateexception E) { Logman. Error ("error while processing freemarker template" + templatefilename, e ); Return false; } Catch (ioexception E) { Logman. Error ("error while generate file" + filename, e ); Return false; } Return true; } |
The code for re-generating an RSS file in the news system is as follows:
/** * Regenerate the RSS file. * * @ Param NID: ID of the updated news. If it is not included in the latest news, RSS. NID is not updated. If it is not included in the latest news, NID is updated. * * @ Return: whether the request is successful */ Private Boolean renewrss (int nid) { List newslist = code for loading news Boolean shouldupdate = false; If (NID> 0) { For (INT I = 0; I <newslist. Size (); I ++) { Newsitem anews = (newsitem) newslist. Get (I ); If (anews. getnid () = NID) { Shouldupdate = true; Break; } } } Else { Shouldupdate = true; } // If not updated, return If (! Shouldupdate) { Return true; } Map root = new hashmap ();
Root. Put ("rssutil", new rssutil ()); Root. Put ("newslist", newslist );
Root. Put ("now", datetime. getnowtimestamp ()); Genefilebyfreemarker ("/news/RSS. FTL", root, propset. getstringprop ("RSS. rssdir"), propset . Getstringprop ("RSS. rssfile"), "UTF-8 "); Return true; } |
Call this renewrss function to add or update or delete news.
(2) JSP dynamic mode
Compared with the static method, it is much simpler, but the efficiency may not be good.
The Action Code of webwork2 is as follows:
Newslist = load news code Return success; |
View JSP is as follows:
<% @ Page contenttype = "text/XML; charset = UTF-8" %> <% @ Taglib uri = "jscud" prefix = "jscud" %> <% @ Taglib uri = "webwork" prefix = "ww" %> <WW: bean name = "'com. jscud. www. util. rssutil '" id = "rssutil"/> <? XML version = "1.0" encoding = "UTF-8"?> & Lt; RSS version = "2.0" & gt; <Channel> <Title> jscud develop </title> <Link> http://www.jscud.com/</link> <Language> ZH-CN </language> <Description> jscud develop by Scud </description> <Webmaster> xxx@21cn.com (SCUD) </Webmaster> <Lastbuilddate> <WW: property value = "# rssutil. nowdatetime"/> </lastbuilddate>
<WW: iterator value = "newslist"> <Item> <Title> <WW: property value = "# rssutil. formatstring2xml (title)"/> </title> <Link> http://www.jscud.com/srun/news/viewhtml/ <WW: property value = "htmlfilepath"/>/<WW: property value = "NID"/>. htm </link> <Pubdate> <WW: property value = "# rssutil. formatrssdate (addtime)"/> </pubdate> <Description> <! [CDATA [ <WW: property value = "# rssutil. formatrsscdata (showcontent)"/> ]> </Description> </Item> </WW: iterator> </Channel> </RSS> |
JSP is much simpler. The above JSP also demonstrates the use of WW: bean.
Many other tool classes are referenced in the above class, which are not listed here. You can implement them by yourself. They are all very simple classes.