velocity 各种判断为null或者“null”或者“”总结

在web开发中,经常会遇到一个需求是,判断变量为空(null)或者空字符串(""),从而影响页面的展示逻辑,velocity中有相应的方法可以判断。当然也可以在java后端转化到有效值再判断。以下是本人在开发中自己总结的,希望对大家有所帮助!
(1)判断null
#if( $name  ==  null)
 something code
#end
(2)判断null或者false
#if( !$name)
 something code
#end
(3)判断null或者空字符串
#if( "$!name" == "")
 something code
#end

一、 彩种(lottery_type) 对应的json 文件

a) 情况:如果彩种(lottery_type) 对应的json 文件存在/ 不存在,

i. 判断方式及结果:

#if($lottery_type_10)
    有开奖结果
#end
#if(!$lottery_type_10)
    无开奖结果
#end

二、 彩期

a) 情况: 彩期(phase) 对应的属性存在/ 不存在

i. 判断方式及结果:

#if($lottery_type_50.phase)
    彩期(phase) 对应的属性存在
#end
#if(!$lottery_type_50.phase)
    彩期(phase) 对应的属性不存在
#end

b) 情况:彩期(phase) 对应的属性存在,但value 是null

i. 判断方式及结果:

#if($lottery_type_50.phase)
    value 不是null
#end
#if(!$lottery_type_50.phase)
    valuenull
#end

c) 情况:彩期(phase) 对应的属性存在,但value 是”null”

i. 判断方式及结果
1.如果是对象通过‘. ’的方式获取属性

#if($lottery_type_50.phase)
    value 不是“null#end
#if(!$lottery_type_50.phase)
    value 是“null

2.如果直接获取属性

#if(${phase} == "null")
    value 是“null#end
#if(${phase} != "null")
    value 不是“null#end

d) 情况:彩期(phase) 对应的属性存在,但value 是“”
i. 判断方式及结果:

#if($lottery_type_50.phase == “”)
    value 为“”
#end
#if($lottery_type_50.phase != “”)
    value 不是“”
#end

e) 实例:

#if($lottery_type_50.phase)
    111
#if($lottery_type_50.phase != "")
    333
#end
#if($lottery_type_50.phase == "")
    444
#end
#if($lottery_type_50.phase != "null")
    555
#end
#if($lottery_type_50.phase == "null")
    666
#end
#end
#if(!$lottery_type_50.phase)
    222
#end
  • 如果彩期(phase) 属性不存在,结果:222
  • 如果彩期(phase) 属性存在,但值为null 或者”null” ,结果:222
  • 如果彩期(phase) 属性存在,但值为”” ,结果:111 444 555
  • 如果彩期(phase) 属性存在,值为”2011036” ,结果:111 333 555

三、 开奖日期

a) 情况:如果开奖日期(draw_time) 属性不存在,或者开奖日期(draw_time) 属性存在,但是值为null 或者 “null ”,

i. 判断方式及结果:

#if($lottery_type_50.draw_time)
    属性不存在或者值为null 或者“null#end
#if(!$lottery_type_50.draw_time)
    属性存在且值bu 为null 也不为“null#end

b) 情况:如果开奖日期(draw_time) 属性存在,值为“”

i. 判断方式及结果:

#if($lottery_type_50.draw_time == “”)
    属性值为””
#end
#if($lottery_type_50.draw_time != “”)
    属性值不为””
#end

c) 实例:

#if($lottery_type_50.draw_time)
    111
#end
#if(!$lottery_type_50.draw_time)
    222
#end
#if($lottery_type_50.draw_time == "")
    333
#end
#if($lottery_type_50.draw_time != "")
    444
#end
#if($lottery_type_50.draw_time == "null")
    555
#end
#if($lottery_type_50.draw_time != "null")
    666
#end
  • 如果开奖日期属性(draw_time) 不存在,结果:222 444 666
  • 如果开奖日期属性(draw_time) 存在,但值为null 或者 “null” ,结果:222 444 666
  • 如果开奖日期属性(draw_time) 存在,但值为”” ,结果:111 333 666
  • 如果开奖日期) 属性(draw_time 存在,并且值为”2011-03-31 22:30:00” ,结果:111 444 666

四、 奖池滚存

a) 情况:奖池(pool_amount) 不存在,或者奖池(pool_amount) 存在,值为null 或者“null ”

i. 判断方式及结果

#if($lottery_type_50.pool_amount)
    奖池(pool_amount) 存在,并且奖池(pool_amount) 值不为null 也不为“null#end
#if(!$lottery_type_50.pool_amount)
    奖池(pool_amount) 不存在,或者奖池(pool_amount) 存在,值为null 或者“null#end

b) 情况:奖池(pool_amount) 值为“”

i. 判断方式及结果

#if($lottery_type_50.pool_amount == "")
    奖池(pool_amount) 值为””
#end
#if($lottery_type_50.pool_amount != "")
    奖池(pool_amount) 值不为””
#end

五、 试机号

a) 情况:fc3d_sjh 不存在,或者fc3d_sjh 存在,值为null 或者”null”
i. 判断方式及说明:

#if($lottery_type_52.fc3d_sjh)
    fc3d_sjh 存在并且值不为null 也不为”null#end
#if(!$lottery_type_52.fc3d_sjh)
    fc3d_sjh 不存在,或者fc3d_sjh 存在,值为null 或者”null#end

b) 情况:fc3d_sjh 存在,值为””

i. 判断方式及说明:

#if($lottery_type_52.fc3d_sjh == “”)
    fc3d_sjh 值为””
#end
#if(!$lottery_type_52.fc3d_sjh != “”)
    fc3d_sjh 值不为””
#end

c) 情况:”fc3d_sjh”:{} 需要进入{} 内进行判断

d) 实例:

#if($lottery_type_52.fc3d_sjh)## 一级属性fc3d_sjh 存在,并且值不为null ,也不为“null “
                          #if($lottery_type_52.fc3d_sjh != "")## 一级属性fc3d_sjh 值不为““
                              #if($lottery_type_52.fc3d_sjh.fc3d_sjh)## 二级属性fc3d_sjh 存在,且值不为null 或者“null “
                                #if($lottery_type_52.fc3d_sjh.fc3d_sjh != "")## 二级属性fc3d_sjh 的值不为““
                                        #if($lottery_type_52.fc3d_sjh.fc3d_sjh.machine || $lottery_type_52.fc3d_sjh.fc3d_sjh.ball
                                            || $lottery_type_52.fc3d_sjh.fc3d_sjh.sjh)## 只要machine 或者ball 或者sjh 存在,且值不为null 或者“null “
                                               #if($lottery_type_52.fc3d_sjh.fc3d_sjh.machine) ## machine 存在,且值不为null 或者“null “
                                                   #if($lottery_type_52.fc3d_sjh.fc3d_sjh.machine != "")##machine 存在,值不为““
                                                       $lottery_type_52.fc3d_sjh.fc3d_sjh.machine
                                                    #else##machine 存在,值为““
                                                        0
                                                    #end
                                                #else## machine 不存在或者存在但是值为null或者“null “
                                                    0
                                                #end#if($lottery_type_52.fc3d_sjh.fc3d_sjh.ball)
                                                   #if($lottery_type_52.fc3d_sjh.fc3d_sjh.ball != "")
                                                       $lottery_type_52.fc3d_sjh.fc3d_sjh.ball
                                                    #else
                                                        0
                                                    #end
                                                #else
                                                    0
                                                #end#if($lottery_type_52.fc3d_sjh.fc3d_sjh.sjh)
                                                    #foreach ($sjh in $lottery_type_52.fc3d_sjh.fc3d_sjh.sjh)
                                                        #if($sjh != "null" && $sjh != "")
                                                            $sjh
                                                        #end
                                                   #end
                                               #end
                                            #else## machine 或者ball 或者sjh 不存在,或者存在但是值为null 或者“null
                                                暂无结果
                                            #end   
                                    #else## 二级属性值为““
                                        暂无结果
                                    #end
                              #else## 二级属性fc3d_sjh 不存在,或者值为null 或者“null “
                                 暂无结果
                              #end
                         #else## 一级属性值为““
                            暂无结果
                         #end
                     #else## 一级属性fc3d_sjh 不存在或者值为null 或者“null “
                        暂无结果
                     #end

六、 开奖结果

a) 情况:开奖结果属性(result) 存在/ 不存在

i. 判断方式及结果:

#if($lottery_type_50.result)
    开奖结果属性(result) 存在
#end
#if(!$lottery_type_50.result)
    开奖结果属性(result) 不存在
#end

b) 情况:开奖结果属性(result) 存在,但值为null

i. 判断方式及结果:

#if($lottery_type_50.result)
    开奖结果属性(result) 存在,值不为null
#end
#if(!$lottery_type_50.result)
    开奖结果属性(result) 存在,但值为null
#end

c) 情况:开奖结果属性(result) 存在,但值为”null”

i. 判断方式及结果:
1.如果是对象通过’.’ 获取对象

#if($lottery_type_50.result)
    开奖结果属性(result) 存在,值不为null
#end
#if(!$lottery_type_50.result)
    开奖结果属性(result) 存在,但值为null
#end

2.如果直接获取对象

#if(result == “null”)
    开奖结果属性(result) 存在,值为”null#end
#if(result != “null”)
    开奖结果属性(result) 存在,但值为”null#end

d) 情况:开奖结果属性(result) 存在,但值为“”

i. 判断方式及结果:

#if($lottery_type_50.result == "")
    开奖结果属性(result) 值为“”
#end
#if($lottery_type_50.result != "")
    开奖结果属性(result) 值不为“”
#end

e) 情况:开奖结果属性(result) 存在,但值为空数组[]

i. 判断方式及结果:

#if($lottery_type_50.result.size() == 0)
    开奖结果 (result) 值不为 []
#end
#if($lottery_type_50.result.size() != 0)
    开奖结果 (result) 值为 []
#end

f) 情况:”result”:[,] ;”result”:[“”,””] ;需要对result 数组的每个元素进行判断

i. “result”:[,] ;”result”:[null,null] ;”result”:[“null”,”null”] ;
1.判断方式及结果

#if($result == "null")
    元素为”null#end
#if($result != "null")
    元素不为”null#end
    结果:元素为”null

ii. “result”:[“”,””]
1.判断方式及结果

#if($result == "")
    元素为””
#end
#if($result != "")
    元素不为””
#end

结果:元素为””

g)

情况:”result”:[,] ;”result”:[{“key”:”red”},{“key”:”blue”}] ;”result”:[{“key”:”red”,”data”:null},{“key”:”blue”,”data”:null}] ;”result”:[{“key”:”red”,”data”:“null ”},{“key”:”blue”,”data”: “null ”}] ;
“result”:[{“key”:”red”,”data”: “”},{“key”:”blue”,”data”: “”}] ;
“result”:[{“key”:”red”,”data”:[]},{“key”:”blue”,”data”:[]}] ;需要对data 进行判断

i. “result”:[{“key”:”red”},{“key”:”blue”}] ;”result”:[{“key”:”red”,”data”:null},{“key”:”blue”,”data”:null}] ;”result”:[{“key”:”red”,”data”: “null ”},{“key”:”blue”,”data”: “null ”}] ;
1.判断方式及结果:

#if($result.data)
    data 属性存在
#end
#if(!$result.data)
    data 属性不存在,或者data 属性存在,值为null 或者 “null#end

ii. “result”:[{“key”:”red”,”data”: “”},{“key”:”blue”,”data”: “”}] ;
1.判断方式及结果:

#if($result.data == "")
    data 属性值为“”
#end
#if($result.data != "")
    data 属性值不为“”
#end

iii. “result”:[{“key”:”red”,”data”:[]},{“key”:”blue”,”data”:[]}]
1.判断方式及结果:

#if($result.data.size() > 0)
    data 数组有数据
#end
#if($result.data.size() == 0)
    data 数组没有数据
#end

h)

情况:”result”:[{“key”:”red”,”data”:[,,,,,]},{“key”:”blue”,”data”:[]}] ;”result”:[{“key”:”red”,”data”:[“”,”“,”“,”“,”“,”“]},{“key”:”blue”,”data”:[“”]}] ;
iv. “result”:[{“key”:”red”,”data”:[,,,,,]},{“key”:”blue”,”data”:[]}]
1.判断方式及结果:

#if($data != "null")
    333
#end
#if($data == "null")
    444
#end

结果:444

v. “result”:[{“key”:”red”,”data”:[“”,”“,”“,”“,”“,”“]},{“key”:”blue”,”data”:[“”]}]
1.判断方式及结果:

#if($data == "")
    为“”
#end
#if($data != "")
    不为“”
#end

i) 实例:

#if($lottery_type_50.result)## 有定义result 属性并且result 属性值不为null ,也不为“null ”
    #if($lottery_type_50.result != “”)
        #if($lottery_type_50.result.size() > 0)## 针对result:[] ,则size = 0 ,而result:[,,],size>0
            #set($exist = "0")## 声明变量$exist ,用于记录是否有开奖结果,初始值为“0#foreach($result in $lottery_type_50.result)
                #if($result != “null” && $result != “”)
                    #if($result.key)
                        #if($result.key == 'red')
                            #if($result.data)
                                #if($result.data != “”)
                                    #if($result.data.size() > 0)
                                        #foreach($data in $result.data)
                                            #if($data != "null" && $data != "")## 如果有data 属性,而且data 属性的值不为null ,不为“null ”,而且非data:[ “”, ””, ””] 此种情况,非data:[,,,] 此种情况
                                                #set($exist = "1")## 变量$exist 赋值“1#end
                                        #end
                                    #end
                                #end
                            #end
                        #end
                        #if($result.key == 'blue')
                            #foreach($data in $result.data)
                                #if($data != "null" && $data != "")
                                    #set($exist = "1")
                                #end
                            #end
                        #end
                    #end
                #end
            #end
##$exist :0 代表无开奖结果,1 代表有开奖结果
#if($exist == "0")
    暂无开奖结果
#else
      #foreach($result in $lottery_type_50.result)
        #if($result != “null” && $result != “”)
            #if($result.key)
                #if($result.key == 'red')
                    #if($result.data)
                        #if($result.data != "")
                            #if($result.data.size() > 0)
                                #foreach($data in $result.data)
                                    #if($data != "null" && $data != "")
                                        <div class="redball">$data</div>
                                    #end
                                #end
                            #end
                        #end
                    #end
                #end
                #if($result.key == 'blue')
                    #if($result.data)
                        #if($result.data != "")
                            #if($result.data.size() > 0)
                                #foreach($data in $result.data)
                                    #if($data != "null" && $data != "")
                                        <div class="redball">$data</div>
                                    #end
                                #end
                            #end
                        #end
                    #end
                #end
            #end
        #end
    #end
#end
        #else##result:[]
            暂无开奖结果
        #end
    #else
        暂无开奖结果
    #end
#else## 未定义result 属性, 或者有定义result 属性,值为null 或者“null ”
    暂无开奖结果
#end

七、 奖项

a) 实例:

#if($result_detail)// 判断result_detail 属性是否存在,如果存在,值是否为null
                   #if($result_detail != "null" && $result_detail != "")// 因为是直接获取属性,需要多加一层”null” 判断
                       #if($result_detail.size() > 0)
                          #set($exists = "0")
                          #foreach ($item in $result_detail)
                              #if($item != "null" && $item != "")// 对数组中每一个奖项的判断
                                  #if($velocityCount)
                                      #if($velocityCount != "null" && $velocityCount != "")
                                          #set($exists = "1")
                                    #end
                                  #end
                             #end
                        #end

                        #if($exists == "1")
                            #foreach ($item in $result_detail)
                                  #if($item != "null" && $item != "")
                                      #if($velocityCount)
                                          #if($velocityCount != "null" && $velocityCount != "")
                                              #if($velocityCount < 3)
                                                #if($velocityCount == 1)
                                                    #set($str = " 一")
                                                #elseif ($velocityCount == 2)
                                                    #set($str = " 二")
                                                #end
                                                <li>${str } 等奖:<span class="money">
                                                    #if(${item.prize})
                                                        #if(${item.prize} != "")
                                                            ${item.prize}
                                                        #else
                                                            0
                                                        #end
                                                    #else
                                                        0
                                                    #end
                                                </span>/</li>
                                            #end
                                        #end
                                      #end
                                 #end
                            #end
                        #else
                            <li> 暂无奖项</li>
                        #end
                       #else
                          <li> 暂无奖项</li>
                       #end
                #else
                    <li> 暂无奖项</li>
                #end
            #else
                <li> 暂无奖项</li>
            #end

八、

九、 总结:

a)   属性不存在或者属性存在但是值为null ,!$prize_amount 结果为true ,prize_amount 结果为false
b)   属性存在值为“null ”,分两种情况:
         i.      一种通过对象. 属性,!$prize_amount 结果为true ,prize_amount 结果为false
       ii.      直接属性,$data == “null” 结果为true ,$data != “null” 结果为false
c)   属性存在但是值为““,$prize_amount == “” 结果为true ,$prize_amount != “” 结果为false
d)   如果是数组,并且数组中有元素,如[,,,,,] ,$data == “null” 结果为true ,$data != “null”结果为false

转载自:http://www.myexception.cn/web/1015628.html
参考:http://blog.csdn.net/happy_cheng/article/details/43769725

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值