1 Freemarker網(wǎng)站靜態(tài)化的實(shí)現(xiàn)(轉(zhuǎn))
首頁(yè):
1.<body>
2.<div id="wrap">
3. <!--頭部開始-->
4. <jsp:include page="/html/top.html" flush="true"></jsp:include>
5. <!--頭部結(jié)束-->
6. <!--導(dǎo)航開始-->
7. <jsp:include page="/html/channel.html" flush="true"></jsp:include>
8. <!--導(dǎo)航結(jié)束-->
9. <jsp:include page="/html/center.html" flush="true"></jsp:include>
10. <!--友情連接開始-->
11. <jsp:include page="/html/index_link.html" flush="true"></jsp:include>
12. <!--友情結(jié)束-->
13. <!--底部開始-->
14. <jsp:include page="/html/bottom.html" flush="true"></jsp:include>
15. <!--底部結(jié)束-->
16.</div>
17.</body>
整個(gè)網(wǎng)站首頁(yè)的基本結(jié)構(gòu)是通過(guò)jsp的include標(biāo)簽將所有通過(guò)freemarker生成的靜態(tài)頁(yè)面組織起來(lái)。后臺(tái)控制各個(gè)部分的靜態(tài)頁(yè)生成。這樣做將首頁(yè)進(jìn)行了拆分,便于了靜態(tài)頁(yè)面的維護(hù),當(dāng)我們需要生成“友情鏈接”部分的時(shí)候就只生成友情鏈接部分,而不需要將整個(gè)頁(yè)面都從新生成一次。
以下是我生成靜態(tài)頁(yè)最核心的方法,使用freemarker。

/** *//**
* 生成靜態(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)單說(shuō)明一下參數(shù):
ServletContext :這個(gè)不用說(shuō)了吧。做java web的應(yīng)該都知道,只不過(guò)struts2中這樣獲取ServletActionContext.getServletContext()
Map<String,Object> data : 模版的數(shù)據(jù)來(lái)源。freemarker通過(guò)一個(gè)Map給ftl模版送數(shù)據(jù)。
現(xiàn)在已友情鏈接為列子詳細(xì)介紹靜態(tài)頁(yè)面如何生成。其他模塊以此類推。
String templatePath : ftl所在的路徑。我這里相對(duì)于網(wǎng)站的一個(gè)相對(duì)路徑然后通過(guò)ServerContext獲取絕對(duì)路徑。
String targetHtmlPath : 最后生成靜態(tài)頁(yè)的路徑:我這里相對(duì)于網(wǎng)站的一個(gè)相對(duì)路徑然后通過(guò)ServerContext獲取絕對(duì)路徑。
友情鏈接根據(jù)這段代碼<jsp:include page="/html/index_link.html" flush="true"></jsp:include>我們需要freemarker生成一個(gè)index_link.html文件。友情鏈接數(shù)據(jù)來(lái)源通過(guò)數(shù)據(jù)庫(kù)查詢獲取。
然后再寫一個(gè)方法專門生成友情鏈接靜態(tài)頁(yè)面:

/** *//**
* 生成友情鏈接的靜態(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è)方法:

/** *//**
* 生成友情鏈接靜態(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();通過(guò)spring注入action的hiberate DAO獲取數(shù)據(jù)給list然后通過(guò)以下代碼
HashMap<String,Object> data = new HashMap<String,Object>();
data.put("links", links);
準(zhǔn)備數(shù)據(jù)調(diào)用createIndexFriendLink()方法。
以下是:ftl模版源碼:
<#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è)依此葫蘆畫瓢。
posted on 2009-11-06 17:52
junly 閱讀(1108)
評(píng)論(2) 編輯 收藏 所屬分類:
java