table

          Freemarker網(wǎng)站靜態(tài)化的實(shí)現(xiàn)

          整個(gè)網(wǎng)站才用struts2 + spring + hibernate + freemarker + urlrewrite完成。首頁(yè)純靜態(tài)化,頻道及其他頁(yè)面通過urlrewrite偽靜態(tài)。現(xiàn)在廢話少說。我先給出首頁(yè)jsp body源代碼:
          Java code
          <body> <div id="wrap"> <!--頭部開始--> <jsp:include page="/html/top.html" flush="true"></jsp:include> <!--頭部結(jié)束--> <!--導(dǎo)航開始--> <jsp:include page="/html/channel.html" flush="true"></jsp:include> <!--導(dǎo)航結(jié)束--> <jsp:include page="/html/center.html" flush="true"></jsp:include> <!--友情連接開始--> <jsp:include page="/html/index_link.html" flush="true"></jsp:include> <!--友情結(jié)束--> <!--底部開始--> <jsp:include page="/html/bottom.html" flush="true"></jsp:include> <!--底部結(jié)束--> </div> </body>

          整個(gè)網(wǎng)站首頁(yè)的基本結(jié)構(gòu)是通過jsp的include標(biāo)簽將所有通過freemarker生成的靜態(tài)頁(yè)面組織起來。后臺(tái)控制各個(gè)部分的靜態(tài)頁(yè)生成。這樣做將首頁(yè)進(jìn)行了拆分,便于了靜態(tài)頁(yè)面的維護(hù),當(dāng)我們需要生成“友情鏈接”部分的時(shí)候就只生成友情鏈接部分,而不需要將整個(gè)頁(yè)面都從新生成一次。
            以下是我生成靜態(tài)頁(yè)最核心的方法,使用freemarker。
          Java code
          /** * 生成靜態(tài)頁(yè)面主方法 * @param context ServletContext * @param data 一個(gè)Map的數(shù)據(jù)結(jié)果集 * @param templatePath ftl模版路徑 * @param targetHtmlPath 生成靜態(tài)頁(yè)面的路徑 */ public static void crateHTML(ServletContext context,Map<String,Object> data,String templatePath,String targetHtmlPath){ Configuration freemarkerCfg = new Configuration(); //加載模版 freemarkerCfg.setServletContextForTemplateLoading(context, "/"); freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8"); try { //指定模版路徑 Template template = freemarkerCfg.getTemplate(templatePath,"UTF-8"); template.setEncoding("UTF-8"); //靜態(tài)頁(yè)面路徑 String htmlPath = context.getRealPath("/html")+"/"+targetHtmlPath; File htmlFile = new File(htmlPath); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8")); //處理模版 template.process(data, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }

          其實(shí)很簡(jiǎn)單,只要Google一下就有很多這方面的代碼。我也是Google的代碼然后自己再根據(jù)實(shí)際情況修改。簡(jiǎn)單說明一下參數(shù):
          ServletContext :這個(gè)不用說了吧。做java web的應(yīng)該都知道,只不過struts2中這樣獲取ServletActionContext.getServletContext()
          Map <String,Object> data : 模版的數(shù)據(jù)來源。freemarker通過一個(gè)Map給ftl模版送數(shù)據(jù)。
          現(xiàn)在已友情鏈接為列子詳細(xì)介紹靜態(tài)頁(yè)面如何生成。其他模塊以此類推。
          String templatePath : ftl所在的路徑。我這里相對(duì)于網(wǎng)站的一個(gè)相對(duì)路徑然后通過ServerContext獲取絕對(duì)路徑。
          String targetHtmlPath : 最后生成靜態(tài)頁(yè)的路徑:我這里相對(duì)于網(wǎng)站的一個(gè)相對(duì)路徑然后通過ServerContext獲取絕對(duì)路徑。

          友情鏈接根據(jù)這段代碼 <jsp:include page="/html/index_link.html" flush="true"> </jsp:include>我們需要freemarker生成一個(gè)index_link.html文件。友情鏈接數(shù)據(jù)來源通過數(shù)據(jù)庫(kù)查詢獲取。
          然后再寫一個(gè)方法專門生成友情鏈接靜態(tài)頁(yè)面:
          Java code
          /** * 生成友情鏈接的靜態(tài)頁(yè)index_link.html * @param context * @param data */ public static void createIndexFriendLink(ServletContext context,Map<String,Object> data){ crateHTML(context,data,"index_link.ftl","index_link.html"); }

          此方法調(diào)用上面的createHTML方法。
          然后根據(jù)以上方法我們就可以再Struts2的action里面從數(shù)據(jù)庫(kù)查詢數(shù)據(jù)放入map調(diào)用createIndexFriendLink()方法生成靜態(tài)頁(yè)了。
          這是action中的一個(gè)方法:
          Java code
          /** * 生成友情鏈接靜態(tài)頁(yè)index_link.html * @return */ public String createLink(){ //權(quán)限驗(yàn)證 if(! this.isAccess()) return "error"; try{ //得到友情鏈接 List links = friendLinkDAO.findAll(); //準(zhǔn)備數(shù)據(jù) HashMap<String,Object> data = new HashMap<String,Object>(); data.put("links", links); //調(diào)用靜態(tài)頁(yè)面方法 HTML.createIndexFriendLink(ServletActionContext.getServletContext(), data); addActionMessage("靜態(tài)頁(yè)面生成成功!"); return "message"; }catch(Exception e){ e.printStackTrace(); return "failure"; } }

          List links = friendLinkDAO.findAll();通過spring注入action的hiberate DAO獲取數(shù)據(jù)給list然后通過以下代碼
          HashMap <String,Object> data = new HashMap <String,Object>();
          data.put("links", links);
          準(zhǔn)備數(shù)據(jù)調(diào)用createIndexFriendLink()方法。
          以下是:ftl模版源碼:
          Java code
          <#if links?size != 0> <div class="link"> <strong>友情鏈接:</strong> <#list links as link> <a href="${link.linkUrl}" target="_blank" title="${link.linkName}">${link.linkName}</a> </#list> </div> <#else> <div class="link"></div> </#if>

          這樣友情鏈接靜態(tài)頁(yè)就生成了。然后其他靜態(tài)頁(yè)依此葫蘆畫瓢。

          總結(jié):雖然靜態(tài)頁(yè)訪問速度快和其他的好處,但實(shí)現(xiàn)起來畢竟還是很麻煩了,維護(hù)也是一個(gè)麻煩事情。如果您的站點(diǎn)更新速度快那么就需要在你的后臺(tái)數(shù)據(jù)更新部分調(diào)用相應(yīng)的createHTML方法實(shí)時(shí)的生成靜態(tài)頁(yè)面。如果更新速度不慢可以在后臺(tái)手動(dòng)更新或者利用操作系統(tǒng)的定時(shí)任務(wù)功能去執(zhí)行你的靜態(tài)頁(yè)面生成程序。

          posted on 2009-12-22 18:52 小卓 閱讀(800) 評(píng)論(0)  編輯  收藏 所屬分類: jsp


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 广河县| 鄂托克前旗| 思南县| 汉中市| 定陶县| 伊川县| 凤阳县| 临潭县| 叙永县| 永修县| 额尔古纳市| 酉阳| 潜江市| 电白县| 松江区| 平阳县| 武冈市| 安龙县| 忻州市| 察哈| 丹阳市| 云和县| 明溪县| 西华县| 綦江县| 夏津县| 台山市| 繁昌县| 昔阳县| 广南县| 呼和浩特市| 绩溪县| 扎赉特旗| 司法| 山阳县| 晋江市| 涪陵区| 灵武市| 庆元县| 全州县| 名山县|