(四)Freemarker 运算符

来源:互联网 发布:排序算法及时间复杂度 编辑:程序博客网 时间:2024/04/30 03:54

   Freemarker 提供了算术运算符, 逻辑运算符, 比较运算符, 空值处理运算符。 

      Freemarker 在进行运算时,你放入map 中的是什么类型, 就会解析成对应的类型。比如说:如果放入的是string 的 10 , 那么解析的时候 依然是string 类型的,而不是说是数值型。


【1. 算术运算符】
    1. 数字运算

        +:  ${a1} + ${a2} = ${a1 + a2}
        -:    ${a1} - ${a2} = ${a1 - a2}
        *:    ${a1} * ${a2} = ${a1 * a2}
        /:    ${a1} / ${a2} = ${a1 / a2}
        %:    ${a1} % ${a2} = ${a1 % a2}
        
    2. 字符串运算:

        +:  ${s1 + "," +  s2 }
        
    3. 混合运算:
        ${s1 + "," + s2 + ":" + a1 + " + " + a2 + " = " + (a1 + a2) }
        
    4. 字符串"10"的结果:${s3 + s3}

【2. 逻辑运算符】 只能用于布尔值,返回也是布尔值,不能直接输出,通常与if ,elseif 指令结合使用
    1. 逻辑与 &&:

        ${(bt && bf)?string}


    2. 逻辑或 ||:

        ${(bt || bf)?string}


    3. 逻辑非 ! :
        ${(!bt)?string}
        
【3. 比较运算符】 只能比较数字和日期,不能比较字符串 ,返回为布尔类型不能直接输出
    1. 运算符类型:
        1. > (gt): 大于号,推荐使用gt
        2. < (lt): 小于号,推荐使用lt
        3. >= (gte): 大于等于, 推荐是用gte
        4. <= (lte): 小于等于,推荐使用lte
        5. == :  等于

        6. != : 不等于


    2. 运算符示例: >
        1. 数值型: ${a1}>${a2}?  ${(a1 > a2)?string}
        2. 日期型:
            ${today?date} > ${tomorrow?date}?  ${(today?date > tomorrow?date)?string }
            ${today?time} > ${tomorrow?time}?  ${(today?time > tomorrow?time)?string }
            ${today?datetime} > ${tomorrow?datetime}?  ${(today?datetime > tomorrow?datetime)?string }
    
【4. 空值运算符】 Freemarker 变量必须赋值,否则会跑出异常。 对此,Freemarker提供了两种方式处理
    1. ??: 判断是否为空,返回布尔类型,如果不为空返回true, 如果为空返回true,不能直接输出

        zong == null? ${(zong??)?string}


    2. !: 设置默认值,如果为空,则设置默认值
        1. 设置默认为空字符串:
            zong = ${zong!}
        2. 设置指定默认值
            zong = ${zong!'hello'}

    

【输出结果】

[html] view plain copy
  1. Freemarker 在进行运算时,你放入map 中的是什么类型, 就会解析成对应的类型。  
  2. 比如说:如果放入的是string 的 10 , 那么解析的时候 依然是string 类型的,而不是说是数值型  
  3. 【1. 算术运算符】  
  4.     1. 数字运算  
  5.         +:  10 + 3 = 13  
  6.         -:  10 - 3 = 7  
  7.         *:  10 * 3 = 30  
  8.         /:  10 / 3 = 3.333  
  9.         %:  10 % 3 = 1  
  10.           
  11.     2. 字符串运算:  
  12.         +:  hello,wold  
  13.           
  14.     3. 混合运算:  
  15.         hello,wold:10 + 3 = 13  
  16.           
  17.     4. 字符串"10"的结果:1010  
  18.   
  19. 【2. 逻辑运算符】 只能用于布尔值,返回也是布尔值,不能直接输出,通常与if ,elseif 指令结合使用  
  20.     1. 逻辑与 &&:  
  21.         false  
  22.     2. 逻辑或 ||:  
  23.         true  
  24.     3. 逻辑非 ! :  
  25.         false  
  26.           
  27. 【3. 比较运算符】 只能比较数字和日期,不能比较字符串 ,返回为布尔类型不能直接输出  
  28.     1. 运算符类型:  
  29.         1. > (gt): 大于号,推荐使用gt  
  30.         2. < (lt): 小于号,推荐使用lt  
  31.         3. >= (gte): 大于等于, 推荐是用gte  
  32.         4. <= (lte): 小于等于,推荐使用lte  
  33.         5. == :  等于  
  34.         6. != : 不等于  
  35.     2. 运算符示例: >  
  36.         1. 数值型: 10>3?  true  
  37.         2. 日期型:  
  38.             2016-4-4 > 2016-4-5?  false  
  39.             17:00:23 > 17:00:23?  false  
  40.             2016-4-4 17:00:23 > 2016-4-5 17:00:23?  false  
  41.       
  42. 【4. 空值运算符】  
  43.     1. Freemarker 变量必须赋值,否则会跑出异常。 对此,Freemarker提供了两种方式处理  
  44.     1. ??: 判断是否为空,返回布尔类型,如果不为空返回true, 如果为空返回true,不能直接输出  
  45.         zong == null? false  
  46.     2. !: 设置默认值,如果为空,则设置默认值  
  47.         zong = hello  
  48.         zong = ab  
  49.       
  50.       
  51.