濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > java  Freemarker頁面靜態(tài)化實(shí)例詳解

java  Freemarker頁面靜態(tài)化實(shí)例詳解

熱門標(biāo)簽:百度地圖添加標(biāo)注圖標(biāo)樣式 南昌市地圖標(biāo)注app 泰州泰興400電話 怎么申請 地圖標(biāo)注市場怎么樣 好操作的電話機(jī)器人廠家 企業(yè)怎么在聯(lián)通申請400電話 南京新思維電話機(jī)器人 如何用中國地圖標(biāo)注數(shù)字點(diǎn) 聊城智能電銷機(jī)器人外呼

Freemarker

FreeMarker 是一個(gè)用 Java 語言編寫的模板引擎,它基于模板來生成文本輸出。FreeMarker與 Web 容器無關(guān),即在 Web 運(yùn)行時(shí),它并不知道 Servlet 或 HTTP。它不僅可以用作表現(xiàn)層的實(shí)現(xiàn)技術(shù),而且還可以用于生成 XML,JSP 或 Java 等。
目前企業(yè)中:主要用 Freemarker 做靜態(tài)頁面或是頁面展示

總結(jié):freemarker 模版引擎,可以使用 Freemarker 模版生成 html 頁面。

Freemarker 語法

 /**
   * freemark入門案例
   * freemark三要素:
   * 1.freemark API
   * 2.數(shù)據(jù)
   * 3.模板文件:ftl文件
   * @throws Exception 
   */
  @Test
  public void test1() throws Exception{
   //創(chuàng)建freemarker核心配置對象,指定freemarker
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定服務(wù)器模板文件所在路徑
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //指定模板文件編碼
   cf.setDefaultEncoding("utf-8");
   //從模板文件路徑下面獲取模板文件對象
   Template template = cf.getTemplate("hello.ftl");

   //創(chuàng)建map對象,封裝模板數(shù)據(jù)
   MapString,Object> maps = new HashMapString,Object>();
   maps.put("hello", "freemarker入門案例");
   //創(chuàng)建一個(gè)輸出對象,把數(shù)據(jù)輸出daoHtml頁面
   Writer out = new FileWriter(new File("F:\\template\\out\\quickstart.html"));
   //生成Html頁面
   template.process(maps, out);

   //關(guān)閉資源
   out.close();
  }

  /**
   * freemarker模板語法處理特殊數(shù)據(jù)格式 
   * 例如:$0.2,20%
   * 模板語法:$0.2:${price?string.currency}
   * 20%:${price?string.percent}
   * @throws Exception 
   */
  @Test
  public void test2() throws Exception{
   //創(chuàng)建freemark核心配置對象,指定freemark版本
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定模板文件所在路徑
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //設(shè)置模板編碼
   cf.setDefaultEncoding("utf-8");
   //從模板文件路徑下面取模板文件對象
   Template template = cf.getTemplate("num.ftl");
   //創(chuàng)建map對象,封裝模板數(shù)據(jù)
   MapString,Object> maps = new HashMap>();
   maps.put("price", 0.2);
   //創(chuàng)建輸出對象,把數(shù)據(jù)輸出到Html頁面
   Writer out = new FileWriter(new File("F:\\template\\out\\price.html"));
   //生成Html頁面
   template.process(maps, out);

   //關(guān)閉資源
   out.close();
  }

  /**
   * 使用模板語法處理null值
   * 模板文件處理語法:
   * 1.?
   * 語法:${username?default("張三")}
   * 2.!
   * 語法:
   * ${username!}
   * ${username!"默認(rèn)值"}
   * 3.if
   * 語法:
   * #if username??>
   * ${username}
   * /#if>
   * @throws Exception 
   */
  @Test
  public void test3() throws Exception{
   //創(chuàng)建freemark核心配置對象,指定freemarker版本
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定模板文件所在路徑
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //設(shè)置模板編碼
   cf.setDefaultEncoding("utf-8");
   //從模板文件路徑下獲取模板文件對象
   Template template = cf.getTemplate("null.ftl");
   //創(chuàng)建map對象,封裝模板數(shù)據(jù)
   MapString,Object> maps = new HashMap>();
   maps.put("username", null);
   //創(chuàng)建輸出對象,把數(shù)據(jù)輸出到html頁面
   Writer out = new FileWriter(new File("F:\\template\\out\\username.html"));
   //生成html頁面
   template.process(maps, out);
   //關(guān)閉資源
   out.close();
  }

  /**
   * 使用模板語法處理pojo數(shù)據(jù)
   * el表達(dá)式獲取數(shù)據(jù):
   * model.addAttribute("p",person);
   * ${p.username}
   * ${p.address}
   * 模板語法獲取pojo數(shù)據(jù)
   * model.addAttribute("p",person);
   * ${p.username}
   * ${p.address}
   * @throws Exception 
   */
  @Test
  public void test4() throws Exception{
   //創(chuàng)建freemark核心配置對象,指定freemarker版本
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定模板文件所在路徑
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //設(shè)置模板編碼
   cf.setDefaultEncoding("utf-8");
   //從模板文件路徑下獲取模板文件對象
   Template template = cf.getTemplate("person.ftl");
   //創(chuàng)建map對象,封裝模板數(shù)據(jù)
   MapString,Object> maps = new HashMap>();
   //創(chuàng)建person對象
   Person person = new Person();
   person.setUsername("張三");
   person.setAge(22);
   maps.put("p", person);
   //創(chuàng)建輸出對象,把數(shù)據(jù)輸出到html頁面
   Writer out = new FileWriter(new File("F:\\template\\out\\person.html"));
   //生成html頁面
   template.process(maps, out);
   //關(guān)閉資源
   out.close();
  }

  /**
   * 使用模板語法處理集合數(shù)據(jù)
   * el表達(dá)式獲取數(shù)據(jù):
   * model.addAttribute("pList",pList);
   * c:foreach item="pList" var="p">
   *  ${p.username}
   *  ${p.age}
   * /c:foreach>
   * 模板語法獲取list數(shù)據(jù)
   * model.addAttribute("pList",pList);
   * #list pList as p>
   *  ${p.username}
   *  ${p.age}
   * /#list>
   * 角標(biāo)語法:${別名_index}
   * @throws Exception 
   */
  @Test
  public void test5() throws Exception{
   //創(chuàng)建freemark核心配置對象,指定freemarker版本
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定模板文件所在路徑
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //設(shè)置模板編碼
   cf.setDefaultEncoding("utf-8");
   //從模板文件路徑下獲取模板文件對象
   Template template = cf.getTemplate("list.ftl");
   //創(chuàng)建map對象,封裝模板數(shù)據(jù)
   MapString,Object> maps = new HashMap>();
   //創(chuàng)建list集合
   ListPerson> pList = new ArrayList>();
   //創(chuàng)建person對象
   Person person1 = new Person();
   person1.setUsername("張三");
   person1.setAge(22);
   //創(chuàng)建person對象
   Person person2 = new Person();
   person2.setUsername("李四");
   person2.setAge(24);
   pList.add(person1);
   pList.add(person2);
   maps.put("pList", pList);
   //創(chuàng)建輸出對象,把數(shù)據(jù)輸出到html頁面
   Writer out = new FileWriter(new File("F:\\template\\out\\list.html"));
   //生成html頁面
   template.process(maps, out);
   //關(guān)閉資源
   out.close();
  }

  /**
   * 使用模板語法處理時(shí)間類型數(shù)據(jù)
   * 獲取數(shù)據(jù)日期:${today?date}
   * 獲取數(shù)據(jù)時(shí)間:${today?time}
   * 獲取數(shù)據(jù)日期時(shí)間:${today?datetime}
   * 獲取數(shù)據(jù)日期時(shí)間格式化:${today?string('yyyy-MM-dd')}
   * @throws Exception 
   */
  @Test
  public void test6() throws Exception{
   //創(chuàng)建freemark核心配置對象,指定freemarker版本
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定模板文件所在路徑
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //設(shè)置模板編碼
   cf.setDefaultEncoding("utf-8");
   //從模板文件路徑下獲取模板文件對象
   Template template = cf.getTemplate("date.ftl");
   //創(chuàng)建map對象,封裝模板數(shù)據(jù)
   MapString,Object> maps = new HashMap>();

   maps.put("today", new Date());
   //創(chuàng)建輸出對象,把數(shù)據(jù)輸出到html頁面
   Writer out = new FileWriter(new File("F:\\template\\out\\date.html"));
   //生成html頁面
   template.process(maps, out);
   //關(guān)閉資源
   out.close();
  }

引入頁面

將另一個(gè)頁面引入本頁面時(shí)可用以下命令完成

Jsp 引入頁面:

Ftl 引入頁面:#include “/include/head.ftl”>

Freemarker 整合 spring

配置整合 Freemarker spring 配置文件:

!-- freemarker交給spring管理 -->
 !-- 使用spring提供模板管理配置對象 -->
 bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  !-- 模板路徑 -->
  property name="templateLoaderPath" value="/WEB-INF/fm/" />
  !-- 模板編碼 -->
  property name="defaultEncoding" value="utf-8" />
 /bean>

創(chuàng)建模版對象

Freemarker 放入服務(wù)器:WEB-INF 文件夾下面:訪問資源文件,必須啟動(dòng)服務(wù)器。

在 web.xml 加載 application 的 spring 配置文件:

!-- 加載springmvc -->
  servlet>
  servlet-name>springmvc/servlet-name>
  servlet-class>org.springframework.web.servlet.DispatcherServlet/servlet-class>
  init-param>
   param-name>contextConfigLocation/param-name>
   param-value>classpath:springmvc.xml,classpath:applicationContext-*.xml/param-value>
  /init-param>
  load-on-startup>1/load-on-startup>
  /servlet>

Nginx訪問

直接訪問,加載不了樣式資源,必須經(jīng)過 http 服務(wù)器,才能加載靜態(tài)資源。

此時(shí) nginx 作為 http 服務(wù)器來訪問靜態(tài)資源。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

您可能感興趣的文章:
  • java、freemarker保留兩位小數(shù)
  • Java用freemarker導(dǎo)出word實(shí)用示例
  • Java模版引擎Freemarker
  • 基于Java的Spring框架來操作FreeMarker模板的示例
  • 使用Java進(jìn)行FreeMarker的web模板開發(fā)的基礎(chǔ)教程
  • Java操作FreeMarker模板引擎的基本用法示例小結(jié)
  • java Spring整合Freemarker的詳細(xì)步驟

標(biāo)簽:開封 白銀 烏蘭察布 臨汾 吉林 山南 自貢 銅川

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《java  Freemarker頁面靜態(tài)化實(shí)例詳解》,本文關(guān)鍵詞  java,amp,nbsp,Freemarker,頁面,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《java  Freemarker頁面靜態(tài)化實(shí)例詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于java  Freemarker頁面靜態(tài)化實(shí)例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    丰原市| 治多县| 中阳县| 余干县| 饶河县| 广德县| 育儿| 阳山县| 盐津县| 锦屏县| 铁岭县| 扎兰屯市| 西吉县| 庐江县| 永定县| 景东| 沈丘县| 定州市| 金门县| 九龙县| 宜川县| 安阳市| 芮城县| 霍山县| 昌乐县| 磐安县| 稷山县| 北安市| 昌邑市| 台湾省| 定西市| 铅山县| 榆社县| 贡觉县| 体育| 宁安市| 平潭县| 长子县| 志丹县| 菏泽市| 靖宇县|