FreeMarker(一)
freemarker的FAQ上面冠冕堂皇的说,未赋值的变量强制抛错可以杜绝很多潜在的错误,如缺失潜在的变量命名,或者其他变量错误。但是实际的效果是:带来的是非常大的编程麻烦,程序里面几乎所有可能出现空值的变量统统需要加上${xxx?if_exists},有些循环条件还需要写if判断,这样不但没有杜绝应该杜绝的错误,反而极大增加了编程的麻烦。
freemarker支持在页面里面直接操作Session,request等,例如${Session[...]},方便确实很方便,但是一旦需要做群集,就会报错。
- Strings: "Foo" or ‘Foo’ or "It’s \"quoted\"" or r"C:\raw\string" 最后一种用法要注意
- Numbers: 123.45
- Booleans: true, false
- Sequences: ["foo", "bar", 123.45], 1..100
- Hashes: {"name":"green mouse", "price":150} 这里注意,lookup name必须是String
- Top-level variables: ${user} action中的getter
- Retrieving data from a hash: user.name, user["name"]
- Retrieving data from a sequence: products[5]
- Special variable: .main(Normally you don’t need to use special variables. They are for expert users.The complete list of special variables can be found in the reference.)
- Interpolation (or concatenation): "Hello ${user}!" (or "Free" + "Marker")
- Getting a character: name[0]
- Concatenation: users + ["guest"]
- Sequence slice: products[10..19] or products[5..]
|
|
|
|
|
|
|
||
|
|
|
|
- Default value: name!"unknown" or (user.name)!"unknown" or name! or (user.name)!
- Missing value test: name?? or (user.name)??
|
Operator group |
Operators |
|---|---|
|
postfix operators |
[subvarName] [subStringRange] . ? (methodParams) expr!default expr! expr?? |
|
unary operators |
+expr -expr !expr |
|
multiplicative |
* / % |
|
additive |
+ - |
|
relational |
< > <= >= (and quivalents: gt, lt, etc.) |
|
equality |
== != (and equivalents: =) |
|
logical AND |
&& |
|
logical OR |
|| |
|
numerical range |
.. |
|
Escape sequence
|
Meaning
|
|
\"
|
Quotation mark (u0022)
|
|
\’
|
Apostrophe (a.k.a. apostrophe-quote) (u0027)
|
|
\\
|
Back slash (u005C)
|
|
\n
|
Line feed (u000A)
|
|
\r
|
Carriage return (u000D)
|
|
\t
|
Horizontal tabulation (a.k.a. tab) (u0009)
|
|
\b
|
Backspace (u0008)
|
|
\f
|
Form feed (u000C)
|
|
\l
|
Less-than sign: <
|
|
\g
|
Greater-than sign: >
|
|
\a
|
Ampersand: &
|
|
\{
|
Curly bracket: {
|
|
\xCode
|