Expression Language and Query Language Compilation - RealWorld-Yuchen-Yang/Notes GitHub Wiki
-
Scriptlet: <% %>, normal java code
-
Directives: <%@ %>, container translation instructions
- page directive: defines page-specific properties,
e.g. <%@ page import="foo.*" session="false" %> - taglib directive: defines tag libraries available to the JSP,
e.g. <%@ taglib tagdir="/WEB-INF/tags/XXX" prefix="XXX" %> - include directive: defines text and code that gets added into the current page,
e.g. <%@ include file="XXX.html" %>
This lets you build reusable chunks(like a standard page heading or navigation bar
that can be added to each page without having to duplicate all that code in each JSP
- page directive: defines page-specific properties,
-
Declarations: <%! %>
Declarations are for declaring members(variables & methods) of generated servlet class
this is added outside the generated service() method,
which means you can declare both static variables and methods
note in the tag, ";" should be pended in the end -
Java expression: <%= %>, prints out result between tags(which is used as println()'s argument)
note in the tag, there should be no tailing ";" -
EL: Syntax: ${ }
It is bad to write Java code in JSP,
to avoid this, we should write EL in the JSP pages and use it to call Java method somewhere else. -
Actions:
- Standard Action:
e.g. <jsp:include page="xxx.jsp"> - Other Action:
e.g. <c:set var="rate" value=32>
- Standard Action:
-
Literal Values Literal values can be wired into bean's property by using
#{ }, a clue to Spring that the content that they contains a SpEL expression,
it can be mixed with non-SpEL values
i.e.
Note: Literal String values can be expressed in SpEL with either ' ' or " " -
Referencing beans, properties and methods SpEL can allow one bean to reference another bean by its ID
i.e.
after getting the reference of the bean, we can also use that reference to extract properties and invoke methods
Note:
The way to avoid the dreaded NullPointerException in SpEL is to use the null-safe accessor '?.'
i.e. <property name="song" value="#{songSelector.selectSoong()?.toUpperCase()}" -
Working with types The key to working with class-scoped methods and constants in SpEL is to use the
T() operator. It will return a Class-typed Object
i.e. -
Performing operations on SpEL values
-
Arithmetic: +, -, *. /, %, ^
-
Relational: <, >, ==, <=, >=,
lt, gt, eq, le, ge
Note: the <, > symbols pose a problem when using these expressions in Spring's XML configuration,
so in the XML, it is better use the textural relational operators -
Logical: and, or, not, !
-
Conditional: ?:(ternary), ?:(Elvis) Note: a common use of the ternary operator is to check for a null value and to wire a default value in the event of a null This can be done by using ?: for clarity
-
Regular expression: matches i.e.
-
Shifting through collections in SpEL
-
Accessing collection members by using the [INDEX or 'KEY'] operator
-
In addition to reading properties from a util:propertise declared collection, Spring makes two special selections of properties available to SpEL:
- systemEnvironment, contains all of the environment variables on the machine running the application
- systemProperties, contains all of the properties that were set in Java as the application started.
(typically using the -D argument)
i.e. -Dapplication.home=/etc/myapp
then you could wire that value into the homePath
-
.?[CONDITION_EXPRESSION] operator, Selecting collection members satisfies the CONDITION_EXPRESSION i.e.
-
.^[CONDITION_EXPRESSION], select first element from the collection satisfying the CONDITION_EXPRESSION .$[CONDITION_EXPRESSION], select last element from the collection satisfying the CONDITION_EXPRESSION
-
.![DESIRED_VALUE_EXPRESSION], projection operator, used to collect a particular property from each collection elements i.e.
-
-
-
Using SpEL should be with caution, for SpEL expressions are ultimately just Strings that are tricky to test
and have no IDE support for syntax checking -
SpEL can be used in both XML and Annotations
Syntax:
Usage Location:
- Syntax $VARIALBE_NAME, Location: build.gradle's " " style string
- 在build.gradle, 以'NAME'格式声明的变量, 会被gradle的parsing缓存, 在整个顺序parsing结束后才会被查询,所以这样使用的变量或方法不用按顺序定义
-
Arithmetic expansion syntax: $((expression))
performs arithmetic operations
-
Brace expansion syntax: {PATTERN{NESTED_PATTERN}}
used as a section of a command line. and expands to a sequence of outputs conforming the above pattern -
Parameter expansion syntax: $PARAMETER_NAME
shell expands this symbol with the value of this variable -
Command substitution syntax: $(COMMAND_WITH_ARGUMENTS) or older version 'COMMAND_WITH_ARGUMENTS'
allows user to use output of the command as an substitution -
Quoting
- Double Quote " ": parameter expansion, arithmetic expansion and command substitution are still available, use backslash() to escape special meanings, i.e. "$", "!", "&", ""
- Single Quote ' ': all expansions are escaped.
Standard Expression Syntax, page17 of usingthymeleaf.pdf
For more information, Thymeleaf 3.0 doc can be found at http://www.thymeleaf.org/documentation.html
- Simple expressions
-
Variable Expressions: ${ }, OGNI(Object-Graph Navigation Language) syntax
-
Selection Variable Expressions: *{ }, evaluates expressons on selected objects rather than on the whole context if there is no selected object, { } expression degenerates to ${ } expression Note: selected means the result of an expression using the th:object attributes it should be used in a subelement of a th:object element. ""represents the parent's variable expression
-
Message Expressions: #{ }
-
Link URL Expressions: @{ }, used as value of URL_TEXT Different types of URLs:
- Absolute URLs: http://www.thymeleaf.org
- Relative URLS:
- page-relative: i.e. user/login.html
- context-relative: /itemdetails?id=3 (context name in server will be added automatically), the directory structure is /webapp/WEB-INF/templates/itemdetails.html
- server-relative: ~/billing.processInvoice (allows calling URLs in another context(application) in the same server)
- protocol-relative: //code.jquery.com/jquery-2.0.3.min.js Note: we can add parameters by using () operator at the end of URL, URL encoding will be automatically performed
-
Fragment Expressions: ~{ }, Fragment expressions are an easy way to represent fragments of markup(HTML, etc.) and move them around templates. This allow page writers to replicate them, pass them to other templates as arguments The most common use is for fragment insertion using th:insert or th:replace i.e.
...commons: Template name main: fragment name Note: there are 3 different fragment formats:- ~{templateName :: selector}, where selector conforms Markup Selector syntax by the AttoParser library, practically, it can be the fragment name, like the above example. ~{templateName :: fragmentName}
- ~{templateName}, include the whole template file
- ~{::selector} or ~{this :: selector}, include the fragment inside the current template
-
- Literals
- Text literals: 'one text', 'Another one!', ..., Only ' need to be escaped by '
- Number literals: 0, 34, 3.0, 12.3, ...
- Boolean literals: true, false
- Null literal: null
- Literal tokens: one, sometext, main, ...
- Text operations
- String concatenation: +
- Literal substitutions: |The name is
${name}|, allows for an easy formatting of strings containing values from variables without the need to append literals with '...' + '...' Note: only *variable expression $ {} is allowed inside Literal substitution |...|
- Arithmetic operations
- Binary operations: +, -, *, /, %, these operators can be appliced inside OGNL variables
- Minus sign (unary operator): -
- Boolean operations:
- Binary operators: and, or
- Boolean negation (unary operator): !, not
- Comparisons and equality:
- Comparators: >, <, >=, <= (gt, lt, ge, le)
- Equality operators: ==, != (eq, ne)
- Conditional operators:
- If-then: (if)?(then)
- If-then-else: (if)?(then):(else)
- Default: (value)?:(defaultValue)
- Special tokens:
- No-Operation: _, the idea behind this token is to specify that the desired result for an expression is to do nothing