<pre cid="n32" mdtype="meta_block" class="md-meta-block md-end-block" style="box-sizing: border-box; overflow: hidden auto; font-family: var(--monospace); font-size: 13.6px; min-height: 0.8rem; white-space: pre-wrap; background: rgb(247, 247, 247); display: block; width: inherit; padding: 1rem; line-height: 1.45; border: 0px; border-radius: 3px; color: rgb(119, 119, 119); margin-top: 0px !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> typora-root-url: images
typora-copy-images-to: images
</pre>
FreeMarker
主要內(nèi)容
FreeMarker 概述
FreeMarker概念
FreeMarker 是一款 模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來生成輸出文本(HTML網(wǎng)頁,電子郵件,配置文件,源代碼等)的通用工具。 是一個Java類庫。
FreeMarker 被設計用來生成 HTML Web 頁面,特別是基于 MVC 模式的應用程序,將視圖從業(yè)務邏輯中抽離處理,業(yè)務中不再包括視圖的展示,而是將視圖交給 FreeMarker 來輸出。雖然 FreeMarker 具有一些編程的能力,但通常由 Java 程序準備要顯示的數(shù)據(jù),由 FreeMarker 生成頁面,通過模板顯示準備的數(shù)據(jù)(如下圖):
FreeMarker不是一個Web應用框架,而適合作為Web應用框架一個組件。
FreeMarker與容器無關,因為它并不知道HTTP或Servlet。FreeMarker同樣可以應用
于非Web應用程序環(huán)境。
FreeMarker更適合作為Model2框架(如Struts)的視圖組件,你也可以在模板中使用
JSP標記庫。
FreeMarker特性
通用目標
能夠生成各種文本:HTML、XML、RTF、Java 源代碼等等
易于嵌入到你的產(chǎn)品中:輕量級;不需要 Servlet 環(huán)境
插件式模板載入器:可以從任何源載入模板,如本地文件、數(shù)據(jù)庫等等
你可以按你所需生成文本:保存到本地文件;作為 Email 發(fā)送;從 Web 應用程序發(fā)送它返回給 Web 瀏覽器
強大的模板語言
所有常用的指令:include、if/elseif/else、循環(huán)結構
在模板中創(chuàng)建和改變變量
幾乎在任何地方都可以使用復雜表達式來指定值
命名的宏,可以具有位置參數(shù)和嵌套內(nèi)容
名字空間有助于建立和維護可重用的宏庫,或將大工程分成模塊,而不必擔心名字沖突
輸出轉換塊:在嵌套模板片段生成輸出時,轉換HTML轉義、壓縮、語法高亮等等;你可以定義自己的轉換
通用數(shù)據(jù)模型
FreeMarker不是直接反射到Java對象,Java對象通過插件式對象封裝,以變量方式在模板中顯示
你可以使用抽象(接口)方式表示對象(JavaBean、XML文檔、SQL查詢結果集等等),告訴模板開發(fā)者使用方法,使其不受技術細節(jié)的打擾
為Web準備
在模板語言中內(nèi)建處理典型Web相關任務(如HTML轉義)的結構
能夠集成到Model2 Web應用框架中作為JSP的替代
支持JSP標記庫
為MVC模式設計:分離可視化設計和應用程序邏輯;分離頁面設計員和程序員
智能的國際化和本地化
字符集智能化(內(nèi)部使用UNICODE)
數(shù)字格式本地化敏感
日期和時間格式本地化敏感
非US字符集可以用作標識(如變量名)
多種不同語言的相同模板
強大的XML處理能力
<#recurse> 和 <#visit> 指令(2.3版本)用于遞歸遍歷XML樹。在模板中清楚和直接的訪問XML對象模型。開源論壇 JForum 就是使用了 FreeMarker 做為頁面模板。
FreeMarker環(huán)境搭建
新建 Maven Web項目
配置坐標依賴和部署插件
pom.xml
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n86" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="xml"><?xml version="1.0" encoding="UTF-8"?>
?
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
?
<groupId>com.xxxx</groupId>
<artifactId>freemarker</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
?
<name>freemarker Maven Webapp</name>
<url>http://www.example.com</url>
?
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
?
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
?
</dependencies>
?
<build>
<finalName>freemarker</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
</plugin>
</plugins>
?
</build>
</project></pre>
修改配置文件 web.xml
在項目的webapp/WEB-INF目錄下的web.xml文件中,添加freemarker 相關 servlet 配置
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n89" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="xml"><?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<init-param>
<param-name>default_encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
</web-app></pre>
編寫Servlet類
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n91" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="java">package com.xxxx.controllter;
?
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
?
@WebServlet("/f01")
public class FreeMarker01 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 添加數(shù)據(jù)
request.setAttribute("msg","Hello FreeMarker!");
// 請求轉發(fā)跳轉到ftl文件中
request.getRequestDispatcher("template/f01.ftl").forward(request,response);
}
}</pre>
新建模板文件 ftl
在webapp目錄下新建template文件夾,創(chuàng)建f01.ftl文件
啟動項目
訪問目
瀏覽器地址欄輸入:http://localhost:8989/f01