1. 程式人生 > >Freemarker之網頁靜態化

Freemarker之網頁靜態化

前言FreeMarker很早就已經瞭解並使用過了,不過離現在已經很久了,很多東西需要再撿起來比較麻煩,因此在這裡做個記錄。

1、什麼是FreeMarker

FreeMarker是一個用Java語言編寫的模板引擎,它基於模板來生成文字輸出。FreeMarker與Web容器無關,即在Web執行時,它並不知道Servlet或HTTP。它不僅可以用作表現層的實現技術,而且還可以用於生成XML,JSP或Java 等。

目前企業中:主要用Freemarker做靜態頁面或是頁面展示

2、FreeMarker的使用方式

把freemarker的jar包新增到工程中。

Maven工程新增依賴

<dependency>

  <groupId>org.freemarker</groupId>

  <artifactId>freemarker</artifactId>

  <version>2.3.23</version>

</dependency>

 

原理:

使用步驟:

第一步:建立一個Configuration物件,直接new一個物件。構造方法的引數就是freemarker對於的版本號。

第二步:設定模板檔案所在的路徑。

第三步:設定模板檔案使用的字符集。一般就是utf-8.

第四步:載入一個模板,建立一個模板物件。

第五步:建立一個模板使用的資料集,可以是pojo也可以是map。一般是Map。

第六步:建立一個Writer物件,一般建立一FileWriter物件,指定生成的檔名。

第七步:呼叫模板物件的process方法輸出檔案。

第八步:關閉流。

模板:

${hello}

      @Test
	public void genFile() throws Exception {
		// 第一步:建立一個Configuration物件,直接new一個物件。構造方法的引數就是freemarker對於的版本號。
		Configuration configuration = new Configuration(Configuration.getVersion());
		// 第二步:設定模板檔案所在的路徑。
		configuration.setDirectoryForTemplateLoading(new File("D:/workspaces-itcast/term197/e3-item-web/src/main/webapp/WEB-INF/ftl"));
		// 第三步:設定模板檔案使用的字符集。一般就是utf-8.
		configuration.setDefaultEncoding("utf-8");
		// 第四步:載入一個模板,建立一個模板物件。
		Template template = configuration.getTemplate("hello.ftl");
		// 第五步:建立一個模板使用的資料集,可以是pojo也可以是map。一般是Map。
		Map dataModel = new HashMap<>();
		//向資料集中新增資料
		dataModel.put("hello", "this is my first freemarker test.");
		// 第六步:建立一個Writer物件,一般建立一FileWriter物件,指定生成的檔名。
		Writer out = new FileWriter(new File("D:/temp/term197/out/hello.html"));
		// 第七步:呼叫模板物件的process方法輸出檔案。
		template.process(dataModel, out);
		// 第八步:關閉流。
		out.close();
	}

3、模板的語法

   訪問map中的key

${key}

   訪問pojo中的屬性

<!--Student物件。學號、姓名、年齡-->

${key.property}

    取集合中的屬性  

<#list studentList as student>
${student.id}/${studnet.name}
</#list>

    取迴圈中的下標

<#list studentList as student>
	${student_index}
</#list>

     判斷

<#if student_index % 2 == 0>
<#else>
</#if>

    日期型別格式化

   

     Null值的處理

    

     Include標籤

     <#include “模板名稱”>

     

  4、FreeMarker整合Spring

       引入需要引用的spring  jar包

       

        建立整合spring的配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
		<property name="defaultEncoding" value="UTF-8" />
	</bean>


</beans>

         測試Controller:

@Controller
public class HtmlGenController {
	
	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;

	@RequestMapping("/genhtml")
	@ResponseBody
	public String genHtml()throws Exception {
		// 1、從spring容器中獲得FreeMarkerConfigurer物件。
		// 2、從FreeMarkerConfigurer物件中獲得Configuration物件。
		Configuration configuration = freeMarkerConfigurer.getConfiguration();
		// 3、使用Configuration物件獲得Template物件。
		Template template = configuration.getTemplate("hello.ftl");
		// 4、建立資料集
		Map dataModel = new HashMap<>();
		dataModel.put("hello", "1000");
		// 5、建立輸出檔案的Writer物件。
		Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html"));
		// 6、呼叫模板物件的process方法,生成檔案。
		template.process(dataModel, out);
		// 7、關閉流。
		out.close();
		return "OK";
	}
}