安装:
网上搜索jbosstools ,进入jbosstools下载JBoss Tools , 注意JBOSS 版本和 Eclipse的版本要匹配.
安装JBoss Tools,
选择JBoss Application Development 下的FreeMarker IDE和Hibernate Tools,如图,
可选择性安装FreeMarker和Hibernate插件
安装完按提示重启Eclipse即可.
应用:
使用maven构建一jar工程,添加依赖freemarker
创建两个freemaker文件
hello.ftl:
student.ftl:
<html>
<head>学生信息</head>
<body>
学生信息列表:<br>
<table border="1">
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<#list studentList as stu>
<#if stu_index % 2==0>
<tr bgcolor="red">
<#else>
<tr bgcolor="green">
</#if>
<td>${stu_index}</td>
<td>${stu.xuehao}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
</tr>
</#list>
</table>
<!-- 可以在key后面使用?date 或者?time或者?datetime ?string(pattern)来告诉模板时间的具体格式-->
当前日期:${date?string('yyyy/MM/dd HH:mm:ss')}
<!-- val! 代表如果val为空 -->
空值:${val!"我是默认值nullnull"}<br/>
空值2:<!-- 判断val2是否为空 ??代表不为空 -->
<#if val2??>
val2有内容,内容是${val2}
<#else>
val2的内容为空
</#if>
<br>----------华丽的分割线-----------------<br>
<#include "hello.ftl">
</body>
</html>
创建一Java实体类Student
package freemarktest;
public class Student {
private int xuehao;
private String name;
private int age;
public Student() {
super();
}
public Student(int xuehao, String name, int age) {
super();
this.xuehao = xuehao;
this.name = name;
this.age = age;
}
//getter and setter方法
}
测试类:
package com.qx.testfreemarker;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarktest.Student;
public class FreeMarkerTest {
public static void main(String[] args) throws Exception {
//加载模板
//1 加载模板的配置对象
Configuration configuration=new Configuration(Configuration.getVersion());
//2 指定模板的目录
configuration.setDirectoryForTemplateLoading(new File("G:\\javaEEWeb_Eclipse\\workspace\\freemarktest\\src\\test\\resources"));
//3 设置字符集
configuration.setDefaultEncoding("UTF-8");
//4 加载具体模板
Template template = configuration.getTemplate("student.ftl");
Map<String, Object> values=new HashMap<>();
values.put("name", "王五");
List<Student> students=new ArrayList<>();
students.add(new Student(1,"张珊珊",20));
students.add(new Student(2, "掌勺七", 12));
students.add(new Student(3, "卢西奥费", 21));
students.add(new Student(4, "洛霞", 21));
values.put("studentList", students);
values.put("date", new Date());
values.put("val", null);
values.put("val2", "你看到的是一个假的空值");
//指定静态化后输出目录
Writer writer=new FileWriter(new File("d:\\freemarker\\22.html"));
template.process(values, writer);
writer.close();
}
}
在 d 盘下创建一文件夹 freemarker
运行测试方法,则在D盘freemarker下生成了22.html文件:
注意:
1.
spring中使用freemarker,要同时添加下面3个依赖.否则编译时就报错
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="freemarkerconfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths" value="G:\javaEEWeb_Eclipse\workspace\freemarktest\src\test\resources"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
</beans>
在手机端,没显示全,特放上图片
2. freemaker的for循环及隔行变色:
注意: 索引是 stu_index而不是stu.index, 这和 jsp不同
3 freemarker的判空 和 include语法