zoukankan      html  css  js  c++  java
  • freemarker使用

    一、添加依赖

      <!--freemarker的核心jar包-->
        <dependency>
          <groupId>org.freemarker</groupId>
          <artifactId>freemarker</artifactId>
          <version>2.3.28</version>
        </dependency>

    二、配置

    springmvc.xml

        <!--FreeMarkerViewResolver是Spring-Context-Support提供的整合类,
           在IOC容器初始化时通知SpringMVC默认使用Freemarker进行数据展示-->
        <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
            <property name="suffix" value=".ftl"/>
            <property name="allowRequestOverride" value="true"/>
            <property name="allowSessionOverride" value="true"/>
            <property name="exposeSessionAttributes" value="true"/>
            <property name="exposeRequestAttributes" value="true"/>
            <property name="contentType" value="text/html;charset=utf-8"/>
        </bean>
        <!--Freemarker设置类-->
        <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
            <!--模板保存目录:resourceswebappWEB-INFftl-->
            <property name="templateLoaderPath" value="/WEB-INF/ftl/"></property>
            <!--其它设置-->
            <property name="freemarkerSettings">
                <props>
                    <prop key="defaultEncoding">UTF-8</prop>  <!--数据进行渲染时使用的字符集-->
    
                </props>
            </property>
            <property name="freemarkerVariables">
                <map>
                    <entry key="root" value="/"/>
                </map>
            </property>
        </bean>

    三、常用语法

    取值:

    ${student} ${student.id}
    
    #{student} #{student.id}


    直接输出字符串

    ${"hello freemarker"}
    
    #{"hello freemarker"}


    if 判断

    <#if student.id == 0>
    
    <h1>0</h1>
    
    <#else if student.id == 1>
    
    <h3>1</h1>
    
    <#else>
    
    error
    
    </#if>


    比较运算符

    =或者==: 判断两个值是否相等.

    !=: 判断两个值是否不等.

    >或者gt: 判断左边值是否大于右边值

    >=或者gte: 判断左边值是否大于等于右边值

    <或者lt: 判断左边值是否小于右边值

    <=或者lte: 判断左边值是否小于等于右边值

    &&: 逻辑与

    ||: 逻辑或

    注意: =和!=可以用于字符串,数值和日期来比较是否相等,但=和!=两边必须是相同类型的值,否则会产生错误

    FreeMarker 区分大小写 "x"=="X"  false


    List 集合数组

    <#list arrays as item>
    
    ${item_index} # 取索引下标
    
    ${item.id} # 取值
    
    </#list>
    
    # 也可以使用<#break>指令跳出迭代


    Map 集合字典 (key必须为字符串)

    <#if map??>
    
        <#list map?keys as key>
    
            key:${key!}
    
            value:${map[key]!}
    
            <#-- 如果value是个对象 ${map[key].id!}-->
    
        </#list>
    
    </#if>


    日期类型转换

    ${date?date} # 解析日期

    ${date?time} # 解析时间

    ${date?datetime} # 解析日期+时间

    自定义格式

    ${date?string('yyyy/MM/dd HH:mm:ss')}


    null值的处理

    !: 指定缺失变量的默认值

    ??: 判断某个变量是否存在

    ${val!} #为null输出空白

    ${val!"默认值"} #指定默认值


    用if判断null值

    <#if val??>

    <h1>有值</h1>

    <#else>

    <h3>val为null</h1>

    </#if>


    include引入外部文件

    <#include "head/hearder.ftl">


    import 引入外部文件

    将引入的文件,放到一个变量中 可以配合macro指令使用

    <#import "head/hearder.ftl" as head>


    assign 声明,赋值

    <#assign name = "郭金鹏">

    <#assign str = "Hello ${user}!">



    算数运算

    <#assign x=5>

    ${ x * x - 100 } # -75 

    ${ x / 2 } # 2.5

    ${ 12 % 10 } # 2


    int 对余数取整

    <#assign x=5>

    ${ (x/2)?int } # 2

    ${ 1.1?int } # 1

    内建函数

    <#assign str="<h1>hello freemarker</h1>">

    ${str ? html} #返回html标签 而不是"<h1>hello freemarker</h1>"字符串

    ${str ? html ? upper_case} #返回标签的内容全大写

    html: 对字符串进行HTML编码

    cap_first: 使字符串第一个字母大写

    lower_case: 将字符串转换成小写

    upper_case: 将字符串转换成大写

    trim: 去掉字符串前后的空白字符

    size: 获取序列中元素的个数

    int: 对余数取整 结果带符号


    macro , nested , return指令

    <#macro book>  //定义一个自定义指令

    j2ee

    </#macro>

    <@book />    //使用刚才定义的指令

    上面的代码输出结果为:    j2ee

    <#macro book booklist>    //定义一个自定义指令booklist是参数

    <#list booklist as book>

      ${book}

    </#list>

    </#macro>

    <@book booklist=["spring","j2ee"] />  //使用刚刚定义的指令

    上面的代码为book指令传入了一个参数值,上面的代码的输出结果为:spring j2ee




  • 相关阅读:
    linux CGI GET POST 用户登录
    linux内核 简化版ksetexample.c解析
    定制.vimrc配置文件
    procfs信息读取实现案例
    基于Extent 的文件存储(fiemap)
    inode_operations介绍
    Linux 文件系统概述
    linux硬链接与软连接的区别
    procfs读写信息实例
    VC 常见问题百问 20080129 13:37 271人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/dominik/p/13372247.html
Copyright © 2011-2022 走看看