Freemarker 页面静态化技术使用入门案例
生活随笔
收集整理的這篇文章主要介紹了
Freemarker 页面静态化技术使用入门案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在訪問 新聞、活動、商品 詳情頁面時, 路徑可以是 xx【id】.html, 服務器端根據請求 id, 動態(tài)生成 html 網頁,下次訪問數據時,無需再查詢數據,直接將 html 靜態(tài)頁面返回。這樣一來就減 少 數 據 庫 交 互 , 從而提高查詢性能。
Struts2 默認使用 freemarker 實現 自定義標簽模板,項目導入 struts2 就已經導入了freemarker jar 包。
使用Freemarker步驟:
1、 安裝 freemarker eclipse 編輯插件。
將 “freemarker_eclipseplugin” 復制 eclipse 的 dropins 文件夾 ,重啟開發(fā)工具。
2、 編輯 freemarker 的模板文件。
通常模板文件 放在 WEB-INF 下 或者 classes 下。
${變量} , 需要在程序中指定變量,合并到模板文件 一同輸出。
hello.ftl模板文件代碼:
<html><title>${title}</title><body>${msg}</body> </html>Java測試類FreemarkerTest代碼:
public class FreemarkerTest {@Testpublic void testOutput() throws IOException, TemplateException {// 配置對象, 配置模板位置Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);configuration.setDirectoryForTemplateLoading(new File("src/main/webapp/WEB-INF/templates"));// 獲取模板對象Template template = configuration.getTemplate("hello.ftl");// 動態(tài)數據對象Map<String, Object> paramterMap = new HashMap<String, Object>();paramterMap.put("title", "你我他學習吧");paramterMap.put("msg", "你好,這是第一個Freemarker案例!");// 合并輸出template.process(paramterMap, new PrintWriter(System.out));} }總結
以上是生活随笔為你收集整理的Freemarker 页面静态化技术使用入门案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery EasyUI window
- 下一篇: Oracle实现数据增删改的sql语句实