In Velocity # set command,
The reference allows the template designer to generate dynamic content, and the Instruction allows the designer to be truly responsible for page presentation and content.
The command starts with #, followed by a keyword, such as if, else, and foreach. This keyword can be placed in the middle. This is a standard writing method, but it may not be easy to understand.
# Set
# The set command is used to assign values to a variable or attribute. The format is # set ($ foo = "bar") or # set ($ foo. bar = $ test ).
LHS must be a variable or attribute, and RHS must be one of the following seven types: Variable, attribute, String Literal Value, numeric literal value, method, ArrayList, and Map. For example:
# Set ($ test = $ test2 + 3)
# Set ($ test. a = "abc ")
# Set ($ test. B = 1)
# Set ($ test. c = $ test.)
# Set ($ test. d = $ test. calc ("1 + 1 "))
# Set ($ test. e = ["123", "abc", $ test. c]) use $ test. e. get (0) to get the value "123"
# Set ($ test. f = {"name": "car", "color": "red"}) $ test. f. get ("color") to get the value "red"
By default, if RHS is null, LHS will not be assigned, and LHS will keep the original value. Therefore, a reference cannot be removed once it is created. However, you can modify a configuration of Velocity to change this phenomenon. Pay special attention to # foreach when making judgments.
When # set is used, if the string literal value is placed in double quotation marks, it will be parsed. For example, for # set ($ test. a = "abc"), the value of $ test. a is abc. # Set ($ test. B = "$ test. a") is equivalent to # set ($ test. B = $ test. ). However, the result of # set ($ test. B = '$ test. A') is to assign the value of $ test. a to $ test. B.
You can use # literal ()... # end to enclose the VTL Code when you need to output a large number of character strings instead of their actual values.