Common Java template engine freemarker labels (2)

Source: Internet
Author: User

1.6 Arithmetic Operators
FreemarkerThe expression fully supports arithmetic operations,FreemarkerThe supported Arithmetic Operators include: +,-, *,/, and %. See the following code:
<# Assign x = 5>
$ {X * X-100}
$ {X/2}
$ {12% 10}
The output result is:
-75, 2.5, 2
When using Arithmetic Operators in expressions, pay attention to the following points:
1. The operation numbers on both sides of the operator must be numbers
2. When the + operator is used, if one side is a number and the other is a string, the number is automatically converted to a string and then connected, for example: ${3 + "5 "}, result: 35
Use the built-in int function to integer the value, for example:
<# Assign x = 5>
${(X/2 )? Int}
$ {1.1? Int}
$ {1.999? Int}
$ {-1.1? Int}
$ {-1.999? Int}
Result: 2 1-1-1
1.7 comparison Operators
The following comparison operators are supported in expressions:
1. = OR =: determines whether two values are equal.
2 .! =: Determines whether two values are unequal.
3.> or GT: Determine whether the left value is greater than the right value.
4.> = or GTE: Determine whether the left value is greater than or equal to the right value
5. <or LT: Determine whether the left value is smaller than the right value
6. <= or LTE: Determine whether the left value is less than or equal to the right value
Note: = and! = Can be used as a string. The value and date are equal, but = and! = The two sides must be values of the same type. Otherwise, an error occurs.FreemarkerThe accuracy ratio is relatively large. "X", "X", and "X" are not equal. other runtimes can act on numbers and dates, but cannot act on strings. Most of the time, using the letter operators such as GT> will have better results, becauseFreemarkerIt can be interpreted as the ending character of the FTL tag. Of course, brackets can also be used to avoid this situation, such as: <# If
(X> Y)>
1.8 logical operators
Logical operators include the following:
Logic and :&&
Logic or: |
Non-logical :!
Logical operators can only act on boolean values; otherwise, errors may occur.
1.9 built-in functions
FreemarkerSome built-in functions are also provided to convert the output, which can be followed by any variable ?,? Follow the built-in functions to rotate the output variables. Below are common built-in string functions:
HTML: HTML encoding of strings
Cap_first: capital the first letter of the string
Lower_case: converts a string to lowercase.
Upper_case: converts a string to uppercase.
Trim: removes the spaces before and after the string.
The following are common built-in functions of the set.
Size: obtains the number of elements in a sequence.
Below are common built-in functions for numeric values
INT: obtains the integer part of A number. The result is signed.
For example:
<# Assign test = "Tom & Jerry">
$ {Test? HTML}
$ {Test? Upper_case? HTML}
Result: Tom & amp; Jerry
1.10 null value Processing Operator
FreemarkerThe processing of null values is very strict,FreemarkerThe variable must have a value. If the variable is not assigned a value, an exception is thrown becauseFreemarkerForce errors of unassigned variables can prevent many potential errors, such as missing potential variable names or other variable errors. the null value mentioned here actually includes non-existent variables. For a Java null value, we think this variable exists, but its value is null, butFreemarkerThe template cannot understand the null value, null value, and non-existent changes.
The quantity is exactly the same.
To process missing variables,FreemarkerTwo operators are provided:
! : Specify the default value of the missing variable
?? : Determines whether a variable exists
Here ,! The following two operators are used:
Variable! Or variable! Defaultvalue. In the first usage, the default value is not specified for the missing variable, indicating that the default value is a Null String, a set with a length of 0, or a map object with a length of 0.
Use! When the default value is specified, the default value type is not required to be the same as the variable type. Use ?? The operator is very simple. It always returns a Boolean value. Usage: Variable ??, If this variable exists, true is returned; otherwise, false is returned.

Related Article

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.