freemarker的基本使用和常用标签

来源:互联网 发布:网页游戏传奇源码下载 编辑:程序博客网 时间:2024/05/29 07:53

freemarker是一种模板语言,能够渲染出我们需要的静态页面,静态页面的访问速度比jsp这种动态页面快的多,而且不需要每次都依靠tomcat进行解析。


用法:(个人总结)

1、取map中的值或者是取pojo中的值

  ${key} ${pojoName.attribute} 


2、注意:在freemarker中的null值会导致生成模板失败,可以这样处理

  ${key!} !表示如果为null则在页面的展示的地方用""空串来展示 ${key!"默认值"} 则在对应的地方显示为默认值


3、取集合中的值(这里我们使用的是#号)

  <#list listName as pojoName></#list>

  取集合中的下标

  ${pojoName_index}


4、格式化日期

  ${date?date}输出日期格式 ${date?time}输出时间格式 ${date?datetime}输出日期时间格式

  ${date?string(pattern)}比如 ${date?string('yyyy/MM/dd HH:mm:ss')}输出标准格式的时间


5、导入页面

  <#include "XXX.ftd"/>


6、if判断

  <#if 条件>

  <#else>

  </#if>


7、判断是否为null

  <#if val??></#if> 表示不为null


下面是我写的测试用例java片段,主要包括了freemarker的基本生成模板的用法:

@Testpublic void testFreeMarker() throws Exception {//1.创建一个模板文件//2.创建一个Configuration对象Configuration configuration = new Configuration(Configuration.getVersion());//3.设置模板所在的路径configuration.setDirectoryForTemplateLoading(new File("F:/taotaoworkspace/taotao-item-web/src/main/webapp/WEB-INF/ftl"));//4.设置模板的字符集,一般utf-8configuration.setDefaultEncoding("utf-8");//5.使用Configuration对象加载一个模板文件,需要指定模板文件的文件名。//Template template = configuration.getTemplate("hello.ftl");Template template = configuration.getTemplate("student.ftl");//6.创建一个数据集,可以是pojo也可以是map,推荐使用mapMap map = new HashMap<>();map.put("hello", "hello freemarker");Student student = new Student(1,"小马",12,"北京北京");map.put("student", student);//学生列表的展示List<Student> stuList = new ArrayList<>();stuList.add(new Student(1,"小马1",11,"北京北京"));stuList.add(new Student(2,"小马2",12,"北京北京"));stuList.add(new Student(3,"小马3",13,"北京北京"));stuList.add(new Student(4,"小马4",14,"北京北京"));stuList.add(new Student(5,"小马5",15,"北京北京"));stuList.add(new Student(6,"小马6",16,"北京北京"));stuList.add(new Student(7,"小马7",17,"北京北京"));map.put("stuList", stuList);//时间的处理map.put("date", new Date());map.put("val", "val值");//小数map.put("number", 123.123);//7.创建一个Writer对象,指定输出文件的路径及文件名。Writer out = new FileWriter(new File("G:/out/student.html"));//8.使用模板对象的process方法输出文件。template.process(map, out);//9.关闭流out.close();}


下面是ftl文件:

<html>  <head>  <title>测试页面</title>  </head>    <body>  学生信息如下:<br/>  id:${student.id}<br/>  age:${student.age}<br/>  name:${student.name}<br/>  address:${student.address}<br/>  学生列表信息  <table border="1">  <tr>  <th>index</th>  <th>id</th>  <th>age</th>  <th>name</th>  <th>address</th>  </tr>  <#list stuList as student>  <#if student_index%2==0>  <tr bgcolor="silver">  <#else>  <tr bgcolor="skyblue">  </#if>  <td>${student_index+1 }</td>  <td>${student.id }</td>  <td>${student.age }</td>  <td>${student.name }</td>  <td>${student.address }</td>  </tr>  </#list>  </table> <!-- ?date  ?time ?datetime ?string(pattern) -->  时间格式的展示: ${date?string("yyyy/MM/dd HH:mm:ss") }<br/>  null值的处理: ${val!"默认值" }<br/>    <!-- 判断是否为null -->  <#if val??>  val值不为null  <#else>  val值为null  </#if>  <br/>    <!-- include -->  <#include "hello.ftl">  <br/>    <!-- 测试保留两位小数 -->  ${number?string("0.00") }  </body></html>


下面是网页效果截图:



原创粉丝点击