Freemarker產生HTML靜態頁面

來源:互聯網
上載者:User

標籤:test   目錄   擷取   模板檔案   cheng   submenu   write   中文亂碼   廣告   

  這段時間的工作是做一個網址導航的項目,面向使用者的就是一個首頁,於是就想到了使用freemarker這個模板引擎來對首頁靜態化。

  之前是用jsp實現,為了避免使用者每次開啟頁面都查詢一次資料庫,所以使用了jsp的內建對象application,在Controller中將資料都查詢出來,

然後放入application,最後在JSP頁面使用jstl標籤配合EL運算式 將資料遍曆出來。這樣做是從一定程度上減輕了伺服器的壓力和頁面的響應速度,

但是仍然沒有靜態頁面響應快。

  使用Freemarker步驟:

  1. jar包,我的項目中使用maven來構建,所以在pom.xml中引入Freemarker jar包的座標就可以了。
  2. ftl模板,我在WEB-INF下面建立一個檔案夾ftl,裡面只放ftl模板檔案,我建立了一個index.ftl檔案。
  3. ftl模板檔案中寫的就是html標籤和css樣式之類的,但是資料部分需要使用Freemarker提供的標籤遍曆出來。如下
                            <!--廣告懸浮-->                        <div class="subMenu">                            <!--工具-->                            <div class=‘xff‘>                                <div class="slideTxtBox">                                    <div class="hd">                                        <span class="arrow"><a class="next"></a><a class="prev"></a></span>                                        <ul>                                            <#list newsMap?keys as testKey>                                                <li>${testKey}</li>                                            </#list>                                        </ul>                                    </div>                                    <div class="bd" style="padding: 5px 10px;">                                        <#list newsMap?values as value>                                            <div style="text-align: left; table-layout: fixed; word-wrap: break-word; width: 100%;" class="baidu">                                            <#list value as newsList>                                                <a target="_blank" href="${newsList.newsurl }" title="${newsList.newsname }">${newsList.newsname }</a>                                            </#list>                                            </div>                                        </#list>                                    </div>                                </div>                            </div>                        </div>

    其中<#list></#list>是Freemarker提供的遍曆標籤,Freemarker提供了很多的標籤,這裡不一一敘述。

  4. Contorller中將資料都查詢出來,通過ftl模板取出資料,最後將完整的資料寫入html
        // 擷取搜尋引擎        List<SearchEngines> searchEngines = this.indexService.findSearchEngines();        // 擷取熱搜客戶        List<Catalog> hotSearchs = this.indexService.findHotSearchs();        // 擷取前25個一級目錄        CatalogCustom custom = new CatalogCustom();        custom.setCatalogLevel(1);        List<Catalog> topLevelCatalog = this.indexService.findCustomers(custom);        // 擷取一級目錄下的前十個客戶        Map<String, List<Catalog>> customerMap = new HashMap<String, List<Catalog>>();                for (Catalog catalog : topLevelCatalog) {            CatalogCustom customer = new CatalogCustom();            customer.setCatalogLevel(3);            customer.setGfid(catalog.getCatalogId());                        List<Catalog> customerList = this.indexService.findCustomers(customer);                            customerMap.put(catalog.getCatalogName(), customerList);                                }        // 擷取新聞相關資料        Map<String, List<News>> newsMap = new HashMap<String, List<News>>();        List<NewsCatalog> newsCatalogs = this.indexService.findNewsCatalog();                for (NewsCatalog newsCatalog : newsCatalogs) {                        News news = new News();            news.setPid(newsCatalog.getId());            List<News> newsList = this.indexService.findNews(news);                        newsMap.put(newsCatalog.getNewscatalog(), newsList);                    }        // 擷取關鍵詞        List<Keywords> keywords = this.indexService.findKeywords();        /*        application.setAttribute("newsMap", newsMap);        application.setAttribute("searchEngines", searchEngines);        application.setAttribute("hotSearchs", hotSearchs);        application.setAttribute("customerMap", customerMap);        application.setAttribute("keywords", keywords);        */                String ftlPath = session.getServletContext().getRealPath("/WEB-INF/ftl");                Configuration configuration = new Configuration();        configuration.setDirectoryForTemplateLoading(new File(ftlPath));        configuration.setDefaultEncoding("UTF-8");        // 擷取或建立一個模版。          Template template = configuration.getTemplate("index.ftl");        // 擷取html靜態頁面檔案        String indexPath = session.getServletContext().getRealPath("/index.html");        //設定檔案輸入資料流編碼,不然產生的html檔案會中文亂碼        FileWriterWithEncoding out = new FileWriterWithEncoding(indexPath,"UTF-8");        // 將頁面中要展示的資料放入一個map中        HashMap<String,Object> map = new HashMap<String, Object>();        map.put("newsMap", newsMap);        map.put("searchEngines", searchEngines);        map.put("hotSearchs", hotSearchs);        map.put("customerMap", customerMap);        map.put("keywords", keywords);        //將map中的資料輸入到index.ftl這個模板檔案中並遍曆出來,最後再將整個模板的資料寫入到index.html中。        template.process(map, out);        out.close();

     

 

    

  

 

Freemarker產生HTML靜態頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.