freemarker將檔案讀寫到HTML中

來源:互聯網
上載者:User

標籤:freemarker   html   

freemarker將檔案讀寫到HTML中


1、設計思路

(1)寫freemarker模板方法

(2)寫測試檔案方法

(3)建立ftl檔案

(4)在指定的路徑下,建立檔案夾


2、寫freemarker模板方法

/**       * 輸出檔案到指定的路徑下       * @Title:printFile       * @Description:       * @param:@param name       * @param:@param root       * @param:@param outputFile       * @return: void       * @throws       */      public void printFile(String name,Map<String,Object> root,String outputFile)      {      FileWriter out = null;      try       {      //寫入到指定的檔案路徑      out = new FileWriter(new File("D:\\MyEclipse\\Maven\\ftl\\" + outputFile));      Template temp = this.getTemplate(name);      try       {      temp.process(root, out);  }       catch (TemplateException e)       {      e.printStackTrace();  }  }       catch (IOException e)       {      e.printStackTrace();  }      finally      {      if(out != null)  try        {//關閉檔案流out.close();    }         catch (IOException e)         {e.printStackTrace();    }      }      }

3、寫測試檔案方法

/** *  * @Title:testFreemarkerFile * @Description: * @param: * @return: void * @throws */@Testpublic void testFreemarkerFile(){//建立資料模型Map<String,Object> root = new HashMap<String,Object>();//為資料模型添加值root.put("username", "張三");root.put("age", "22");root.put("sex", "男");//將資料模型和模板中的資料輸出到控制台ft.printFile("user.ftl", root,"user.html");}

4、建立ftl檔案

姓名:${username}年齡:${age}性別:${sex}

5、建立檔案夾

D:\MyEclipse\Maven\ftl


6、產生結果

(1)產生user.html



(2)控制台產生的結果

姓名:張三年齡:22性別:男




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.