Velocity various judgements are null or "null" or "" Summary

Source: Internet
Author: User
In web development, there is often a need to determine whether a variable is empty (null) or an empty string (""), which affects the presentation logic of the page, and there is a corresponding method in velocity that can be judged. Of course, you can also convert the Java backend to a valid value before judging. The following is my own summary in the development, I hope to help you.
(1) determine null
#if ($name  =  null)
 something code
#end
(2) to determine null or FALSE
#if (! $name) The
 something Code
#end
(3) determines null or an empty string
#if ("$!name" = "")
 something code
#end
A, color type (lottery_type) the corresponding JSON file a) Case: if the color type (lottery_type) corresponds to the JSON file exists/does not exist,

I. Methods of judgement and results:

#if ($lottery _type_10)
    has lottery results
#end
#if (! $lottery _type_10)
    without lottery results
#end
second, the color period a) Condition: Color period (phase) the corresponding attribute exists/does not exist

I. Methods of judgement and results:

#if ($lottery _type_50.phase)
    color period (phase) the corresponding property exists
#end
#if (! $lottery _type_50.phase)
    color period (phase) The corresponding property does not exist
#end
b) Condition: Color period (phase) the corresponding property exists, but value is null

I. Methods of judgement and results:

#if ($lottery _type_50.phase)
    value is not
a null #end
#if (! $lottery _type_50.phase)
    value is null
#end
c) Condition: Color period (phase) the corresponding property exists, but value is "null"

I. Methods of judgement and results
1. If the object is passed '. ' Way to get properties

#if ($lottery _type_50.phase)
    value is not a "null"
#end
#if (! $lottery _type_50.phase)
    value is "null"

2. If you get the property directly

#if (${phase} = = "null")
    value is a "null"
#end
#if (${phase}!= "null")
    value is not "null"
#end

D) Condition: the color period (phase) corresponds to the attribute exists, but value is ""
I. Methods of judgement and results:

#if ($lottery _type_50.phase = = "")
    value is ""
#end
#if ($lottery _type_50.phase!= "")
    value is not ""
#end

e) Examples:

#if ($lottery _type_50.phase)
#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
If the color period (phase) property does not exist, result: 222 If the color period (phase) attribute exists, but the value is null or "null", the result: 222 If the color period (phase) attribute exists, but the value is "", Result: 111 444 555 If the color period (phase) attribute exists , the value is "2011036", and the result: 111 333 555 third, lottery date a) Situation: if the Lottery date (Draw_time) attribute does not exist, or the lottery date (Draw_time) attribute exists, but the value is null or "null",

I. Methods of judgement and results:

#if ($lottery _type_50.draw_time)
    property does not exist or the value is null or "null"
#end
#if (! $lottery _type_50.draw_time)
    property exists and the value BU is null and not "null"
#end
(b) Situation: If the Lottery date (Draw_time) attribute exists, the value is ""

I. Methods of judgement and results:

#if ($lottery _type_50.draw_time = = "")
    property value is ""
#end
#if ($lottery _type_50.draw_time!= "")
    property value is not ""
#end
c) Examples:
#if ($lottery _type_50.draw_time)
#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
If the lottery date attribute (draw_time) does not exist, the result: 222 444 666 If the Lottery date property (Draw_time) exists, but the value is null or "null," The result: 222 444 666 If the Lottery date attribute (draw_time) exists, but the value is "", Result: 111 333 666 If the Lottery date attribute (Draw_time exists, and the value is "2011-03-31 22:30:00", results: 111 444 666 four, the prize pool rollover a) Situation: the prize pool (pool_amount) does not exist, or the prize pool (Pool_amount) exists, the value is null or "null"

I. Methods of judgement and results

The #if ($lottery _type_50.pool_amount)
    prize Pool (Pool_amount) exists and the prize pool (Pool_amount) value is not NULL or "null"
#end
#if (!$ The Lottery_type_50.pool_amount)
    prize Pool (pool_amount) does not exist, or the prize pool (Pool_amount) exists, the value is null or "null"
#end
(b) Situation: The prize pool (pool_amount) value is ""

I. Methods of judgement and results

#if ($lottery _type_50.pool_amount = = "")
    Prize Pool (pool_amount) value is ""
#end
#if ($lottery _type_50.pool_amount!= "") the
    value of the pool (Pool_amount) is not ""
#end
Five, test machine number

A) Condition: FC3D_SJH does not exist, or FC3D_SJH exists, the value is null or "null"
I. Method of judgement and Description:

The #if ($lottery _type_52.fc3d_sjh)
    FC3D_SJH exists and the value is not NULL or "null"
#end
#if (! $lottery _TYPE_52.FC3D_SJH
    FC3D_SJH does not exist, or FC3D_SJH exists, the value is null or "null"
#end
(b) Situation: FC3D_SJH exists, value is ""

I. Method of judgement and Description:

#if ($lottery _TYPE_52.FC3D_SJH = = "")
    FC3D_SJH value is "
" #end
#if (! $lottery _type_52.fc3d_sjh!= "")
    Fc3d_ The SJH value is not ""
#end
c) Situation: "FC3D_SJH": {} need to enter {} for judgment d) Examples:
#if ($lottery _TYPE_52.FC3D_SJH) # # First-level attribute FC3D_SJH exists and the value is not null and is not a "null" #if ($lottery _type_52.fc3d _SJH!= "") # # First-level property FC3D_SJH value is not "" #if ($lottery _type_52.fc3d_sjh.fc3d_sjh) # # Two properties fc3d_sjh exist, and
                                        Value is not NULL or "null" #if ($lottery _type_52.fc3d_sjh.fc3d_sjh!= "") # Two-level property FC3D_SJH value is not ' " #if ($lottery _type_52.fc3d_sjh.fc3d_sjh.machine | | $lottery _type_52.fc3d_sjh.fc3d_sjh.b all | |
                                               $lottery _type_52.fc3d_sjh.fc3d_sjh.sjh) # # # As long as machine or ball or SJH exist, and the value is not NULL or ' null '
                                                   #if ($lottery _type_52.fc3d_sjh.fc3d_sjh.machine) # # machine exists and the value is not NULL or ' null '
                                                       #if ($lottery _type_52.fc3d_sjh.fc3d_sjh.machine!= "") # #machine exists, the value is not ""
                         $lottery _type_52.fc3d_sjh.fc3d_sjh.machine                           #else # #machine exists with a value of ' 0 #end #else # # Machine does not exist or exists but the value is null
                                                or "null" 0 #end Machine #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 Ball #if ($lottery _type_52.fc3d_sjh.fc3d_sjh.sjh) #fo  Reach ($sjh $lottery _type_52.fc3d_sjh.fc3d_sjh.sjh) #if ($SJH!=
                                                        "Null" && $sjh!= "") $SJH
                                               #end #end
                                                #end #else # # machine or ball or SJH does not exist, or exists but the value is null or null   
                                    No results #end
                           #else # # Two property value is "" No results #end   #else # # Two property fc3d_sjh does not exist, or the value is null or "NULL" has no results #end
                     #else # # First-level property value is "" No results #end #else # # First-level property FC3D_SJH does not exist or the value is null or "NULL" has no results #end
Vi. Lottery Results a) Situation: Lottery results attribute (result) exists/does not exist

I. Methods of judgement and results:

#if ($lottery _type_50.result)
    Lottery Results Property (result) exists
#end
#if (! $lottery _type_50.result)
    Lottery Results Property (Result ) does not exist
#end
situation: Lottery results attribute (result) exists, but the value is null

I. Methods of judgement and results:

#if ($lottery _type_50.result)
    Lottery Results attribute (result) exists, the value is not null
#end
#if (! $lottery _type_50.result)
    The lottery results attribute (result) exists, but the value is null
#end
c) Condition: Lottery Results attribute (result) exists, but the value is "null"

I. Methods of judgement and results:
1. If the object gets the object through '. '

#if ($lottery _type_50.result)
    Lottery Results attribute (result) exists, the value is not null
#end
#if (! $lottery _type_50.result)
    The lottery results attribute (result) exists, but the value is null
#end

2. If you get the object directly

#if (Result = = "null")
    Lottery Results Property (outcome) exists with a value of "null"
#end
#if (result!= "null")
    lottery Results attribute (outcome) exists, but the value is " Null "
#end
d) Condition: Lottery Results attribute (result) exists, but the value is ""

I. Methods of judgement and results:

#if ($lottery _type_50.result = = "")
    Lottery Results Property (result) value is ""
#end
#if ($lottery _type_50.result!= "")
    The lottery Results property (result) value is not ""
#end
e) Condition: Lottery Results attribute (result) exists, but the value is an empty array []

I. Methods of judgement and results:

#if ($lottery _type_50.result.size () = = 0)
    lottery Results (Result) value is not []
#end
#if ($lottery _type_50.result.size ( != 0)
    lottery Results (Result) value is []
#end
f) Case: "Result": [,]; "Effect": ["," "];

I. "Result": [,]; ' Result ': [null,null]; "Result": ["null", "null"];
1. Method of judgment and result

#if ($result = = "null")
    element is "null"
#end
#if ($result!= "null") element is
    not "null"
#end
    Result: element is "null"

II. "Result": ["", ""]
1. Method of judgment and result

#if ($result = = ")
    element is" "
#end
#if ($result!=" ")
    element is not" "
#end

Result: Element is "" g)

Situation: "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 must be judged

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. Method of judgment and result:

#if ($result. Data)
    Data property exists
#end
#if (! $result. Data) The Data property
    does not exist, or the data property exists, the value is null or "null"
# End

II. "Result": [{"Key": "Red", "Data": ""},{"key": "Blue", "Data": ""}];
1. Method of judgment and result:

#if ($result. Data = "") The
    value of the Data property is ""
#end
#if ($result. Data!= "") the value of the
    data property is not ""
#end

III. "Result": [{"Key": "Red", "Data": []},{"Key": "Blue", "Data": []}]
1. Method of judgment and result:

#if ($result. data.size () > 0) The
    data array has
#end
#if ($result. data.size () = = 0)
    No data array
End
h)

Condition: "Result": [{"Key": "Red", "data": [,,,,,]},{"key": "Blue", "Data": []}]; "Result": [{"Key": "Red", "Data": ["," "," "," "", "", "" " , "", ""]},{"key": "Blue", "Data": ["]}];
IV. "Result": [{"Key": "Red", "data": [,,,,,]},{"key": "Blue", "Data": []}]
1. Method of judgment and result:

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

Results: 444

V. "Result": [{"Key": "Red", "Data": ["," "," "," "," "," "", "" "", "" "" "" "]},{" "," "" "" ""
1. Method of judgment and result:

#if ($data = = "") is "
    "
#end
#if ($data!= "") is not ""
#end
i) Examples:
#if ($lottery _type_50.result) # # has a defined result property and the result property value is not null and is not a ' null ' #if ($lottery _type_50.result!= "") # if ($lottery _type_50.result.size () > 0) # # for result:[], size = 0, and result:[,,],size>0 #set ($exist = "0") #  # declare variable $exist to record whether there is a lottery result, the initial value is "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!= "") # # If there is a data property and the value of the data property is not NULL, not ' Nu ll ", and not data:[" "," "," "" "in this case, non-data:[,,,] This case #set ($exist =" 1 ") # # variable $e
            Xist Assignment "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 represents no lottery results, 1 delegates have lottery results #if ($exist = = "0") No lottery results #else #foreach ($result in $lottery _type_ 50.result) #if ($result!= "null" && $result!= "") #if ($result. Key) #if ($resul
                            T.key = = ' Red ') #if ($result. Data) #if ($result. Data!= "") #if ($result. Data. Size () > 0) #foreach ($data in $result. Data) #if ($d 
                                    ATA!= "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: [] No lottery results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.