一:freemarker是什麼?html
freemarker是一個模板引擎,基於定義的模板和數據生成對應的文本(HTML,xml,java等),是一個生成文本的工具。java
二:freemarker的使用方法web
(1)在工程中引入freemarker相關的依賴spring
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
(2)使用的步驟app
第一步:建立一個Configuration對象,直接new一個便可,構造參數是freemarker的版本號webapp
第二步:設置模板文件所在的路徑,須要給出在磁盤上儲存的全路徑ide
第三步:設置生成的文件的編碼格式,通常爲utf-8格式函數
第四步:加載模板,建立模板對象工具
第五步:建立模板使用的數據集,可使pojo也能夠是map類型的測試
第六步:建立Write流對象,將文件文件輸出,須要指定生成的文件的名稱
第七步:調用模板的process方法,生成相應的文本
第八步:關閉流
@Test public void genFile() throws Exception { // 第一步:建立一個Configuration對象,直接new一個對象。構造方法的參數就是freemarker對於的版本號。 Configuration configuration = new Configuration(Configuration.getVersion()); // 第二步:設置模板文件所在的路徑。 configuration.setDirectoryForTemplateLoading(new File("D:/workspace/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/out/hello.html")); // 第七步:調用模板對象的process方法輸出文件。 template.process(dataModel, out); // 第八步:關閉流。 out.close(); }
(3)模板的語法
1.訪問map中的key
${key}便可得到對應的value值
2.訪問pojo中的屬性
以student對象爲例: ${student.id} ${student.name}便可取得student對象中的id值和name值
3.去集合中的元素
例如:遍歷學生對象集合,取出每個學生的id值和name值
<#list studentList as student>
${student,id} ${student.name}
<#list>
4.取集合中的下標
<#list studentList as student>
對象+下劃線+index 便可得到下標值
${student_index}
<#list>
5.判斷
<#list sutdnetList as student>
進行奇偶數的判斷
<#if student_index % 2 ==0>
//偶數的處理過程
<#else>
//奇數的處理過程
<#if>
<#list>
6.日期類型的格式化
${date?date} 當前日期
${date?time} 當前時間
${date?datetime} 當前日期和時間
${date?string("yyyy-MM-dd hh:mm:ss")} 設置日期格式
7.NULL的處理
!對輸出的控制處理,只輸出,無返回值
${name} 若是name爲空就會報錯
${name!}若是name爲空,不會報錯,沒有輸出
${name!"默認值'} 若是name爲空,就會輸出默認值
${name!666}若是name爲空,會輸出666
${student.name}若是student或者name爲空,報錯
${student.name!"默認值"}若是student爲空,會報錯,name爲空,輸出默認值
??測試是否爲null,返回Boolean類型的值
product.color??將只測試color是否爲null
(product.color)??將測試product和color是否存在null
??和?的區別
??是判斷對象是否爲空,例如:<#if object??>object對象不爲空(即object存在)</#if>
?後面要加內建函數名,例如:<#if object?exists>object對象不爲空(即object存在)</#if>
<#if str??>${str?string}</#if><#--將str以字符串形式顯示-->
8.include標籤
<#include 「模板名稱「>
三:freemarker和spring的整合
(1)將Configuration對象的建立交給spring統一管理,爲該對象注入兩個屬性,模板路徑和文件的編碼格式
<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>
(2)進行測試
@Controller public class HtmlGenController { @Autowired private FreeMarkerConfigurer freeMarkerConfigurer; @RequestMapping("/genhtml") @ResponseBody public String genHtml()throws Exception { // 一、從spring容器中得到FreeMarkerConfigurer對象。 // 二、從FreeMarkerConfigurer對象中得到Configuration對象。 Configuration configuration = freeMarkerConfigurer.getConfiguration(); // 三、使用Configuration對象得到Template對象。 Template template = configuration.getTemplate("hello.ftl"); // 四、建立數據集 Map dataModel = new HashMap<>(); dataModel.put("hello", "1000"); // 五、建立輸出文件的Writer對象。 Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html")); // 六、調用模板對象的process方法,生成文件。 template.process(dataModel, out); // 七、關閉流。 out.close(); return "OK"; } }
錯誤的地方但願你們指正。