freemarker的学习和使用

来源:互联网 发布:淘宝v任务 编辑:程序博客网 时间:2024/05/29 07:51
1、freemarker是什么
      freemarker是一款模板引擎,它实现程序逻辑和页面设计的分离,完成静态文本的输出。它不是web开发框架,准确来说,它仅仅只是我们MVC开发下,视图层的一个组件而已。

2、为什么要用freemarker
     使用freemarker实现逻辑和表现的分离,使java程序员和美工能够各司其职,发挥所长,只专注自己的擅长的方面。并且它的页面是静态化的,方便搜索引擎找到。

3、freemarker的模板(ftl文件)构成
    (1)、Text文本:文本会照着原样来输出。
    (2)、Interpolation 插值:这部分的输出会被计算的值来替换。插值由${和}所分隔(或者#{和},这种风格已经不建议再使用了)。
    (3)、FTL tags标签:FTL标签和HTML标签很相似,但是它们却是给FreeMarker的指示,而且不会打印在输出内容中。
    (4)、Comments注释:FTL的注释和HTML的注释也很相似,但它们是由<#--和-->来分隔的。注释会被FreeMarker所忽略,更不会在输出内容中显示。
     我们来看一个具体的模板,其中的内容已经用颜色来标记了:文本,插值,FTL标签,注释。    
<html><head><title>Welcome!</title></head><body><#-- Greet the user with his/her name --><h1>Welcome ${user}!</h1><p>We have these animals:<ul><#list animals as being><li>${being.name} for ${being.price} Euros</#list> </ul></body></html>

4、freemarker的使用
    说明:这里的freemarker使用示例,是在springMVC框架下,结合maven自动化构建的示例。
    (1)maven引包
<dependency><groupId>org.freemarker</groupId>         <artifactId>freemarker</artifactId>         <version>2.3.16</version></dependency>
     (2)编写前台模板展示页面
       这里只给出一些html片段和ftl模板文件,首先是html片段a.html,然后是ftl模板文件b.ftl
<div class="module-one"><div class="mode-left">   <div class="mode-left-tit clearfix">      <h1 class="mo-tit-h1 font">行业资讯</h1>      <span class="span-more"><a href="../../mvc/web/news.html">更多>></a></span>   </div>   <div class="mode-left-con home-mode-h"><#include "/res/web/template/index-adt4.ftl">     <div class="mode-list3" id="mode-list3"><!--  行业资讯  -->    <#include "/res/web/template/index-infomations.ftl">             </div>         </div><pre name="code" class="html">      </div>
 </div>
<#list infomations as info><#if info_index ==0><div class="mode-list3-tab" style="height:235px;overflow:hidden"><#else><div class="mode-list3-tab hide"  style="height:235px;overflow:hidden"></#if><h1 class="font"><a target="_blank" href="../../mvc/web/newsDetail.html?newId=${info.id}">${info.title}</a></h1> <#if info.content?length gt 200>${info.content[0..200]}......<a target="_blank" style="color:#e50011;"   href="newsDetail.html?newId=${info.id}">[ 详情 ]</a>  <#else>${info.content} </#if> </div></#list>
    我们是怎样来显示引入了ftl模板文件的html页面呢?首先,我们要在浏览器地址栏中输入向后台发送请求的路径,比如localhost:8080/mvc/web/index.html,然后服务器后台查询数据,并将数据插入模板文件里,最后输出到页面。  
  (3)向后台发送请求
@RequestMapping(value="/index")public ResponseEntity<String> index(){try {Map<String,Object> data = new HashMap<String,Object>();//行业资讯data.put("infomations", getInfomation());return super.getFreeMark("/res/web/index.html", data);} catch (IOException e) {return new ResponseEntity<String>(e.getMessage(),HttpStatus.NOT_FOUND);}}
    controller层的这个方法最关键的地方是super.getFreeMark( string url, map data),它将对引入了模板文件的html进行组装数据,其工作流程是先通过freeMarker加载模板,然后获取模板,再将要插入的数据插入到模板中,最后是将获得的模板+数据通过html的response响应流输出到页面。super.getFreeMark( string url, map data)的具体实现,我会在接下来的文章中讨论。
    这里再解释一下上述代码中所使用的freeMarker标签   
<#include "/res/web/template/index-infomations.ftl">
    在html页面中引入ftl模板文件
<#list infomations as info></#list>
    “infomations”是服务器端查询的List集合数据,freeMarker解析遇到这个标签时,它会循环输出<#list></#list>标签之内的内容。
<#if info_index ==0><div class="mode-list3-tab" style="height:235px;overflow:hidden"><#else><div class="mode-list3-tab hide"  style="height:235px;overflow:hidden"></#if> </div>
     <#if></#if>为判断标签,这里表示如果info_index等于0,解析为”<div class="mode-list3-tab" style="height:235px;overflow:hidden">“,否则解析为”<div class="mode-list3-tab hide"  style="height:235px;overflow:hidden">“,"info_index"中的info是上边List集合infomations中的元素,index是表示循环这个list的下标,索引。

5、freemarker学习笔记
(1)freemarker不允许值丢失,即当值为空或者null的时候,会报错。
        解决办法:在值后面加!"缺省值"
        <h1>Welcome ${user.name}!"缺省值"</h1>
(2)??表示user为空,则不输出if内的内容
        <#if user??><h1>Welcome ${user.name}!"缺省值"</h1></#if>
(3)返回回来的值可以计算,但它必须是数值型
        ${cargo.weight/2}
(4)
<#if>标签表达式有逻辑操作:
        逻辑或:||
        逻辑与:&&
        逻辑非:!
        注意:逻辑操作符仅仅在布尔值之间有效,若用在其他类型将会产生错误导致模板执行中止。
(5)当你需要输出<,&像这样的特殊符号,把插值放在escape指令中,它会输出&amp;










0 0