The role of EL expressions
EL is used to render data in JSP pages without using scripts, declarations, expressions
EL use location
Cannot be used in directives, directives are executed at compile time, and EL is executed at page rendering time
如:<%@ include%>、<%@ page%>等
Available in HTML, JSP tags, JS, CSS
EL syntax
1. Execute immediately -> parsed and executed by the JSP engine when the page is rendered (JSP is executed from top to bottom)
${expr}
2. Delayed execution -> used in JSF, legal in JSP, but not recommended
#{expr}
EL expressions must generate a certain value after execution, and cannot declare variables and execute assignment statements in expressions.
reserved keywords
true, false, null, instanceof-> have the same meaning as keywords in Java
empty->empty returns true; non-empty returns false ${empty x}
The parentheses are the corresponding operators, which can be used instead
div( / )、mod( % )、and( && )、or( || )、not( ! )、eq( == )、ne ( != )、It( < )、gt ( > )、le( <= )、ge ( >= )
Operator precedence (top-down)
literal
String literals in EL can be caused by ' or ''
HashSet<Object> literal: ${ object.method({1,2,'three',4.00,x}) }, the elements in the Set are caused by {}
ArrayList<Object> literal: ${ object.method([1,2,'three',4.00,x]) }, the elements in the List are caused by []
HashMap<Object,Object> literal: ${ object.method({'one':1,'two':2,'three':x,'list':[1,2.3]}) }, in Map Elements are quoted with {}
Object Properties and Methods
The Student class has a student variable and a num attribute. The num attribute provides set and get methods.
The num property can be accessed using ${student.num}, which is actually the getNum() method called
The num property can be accessed using ${student["num"]}
You can directly call the method of the object, ${student.toString()}
Access to static fields and static methods -> use the class name under the lang package, and other packages use the JSP page instruction to import the package or write out the fully qualified class name
${Integer.MAX_VALUE}, access the static field MAX_VALUE in the Integer class
${Integer.reverse(24)}, access the static method reverse in the Integer class
lambla expression -> consistent with the usage in java
${v = (a,b)->a+b;v(3,15)}, the final value is 18
Scoped variables in EL expressions
9 implicit variables in JSP: request, response, session, out, application, config, pageContext, page, exception
11 implicit variables in EL: pageContext, pageScope, requestScope, sessionScope, applicationScope, param, paramValues, header, headerValues, cookie, initParam
hidden object |
Types of |
illustrate |
PageContext |
javax.servlet.ServletContext |
the PageContext representing this JSP |
PageScope |
java.util.Map |
Get the value corresponding to the property name of the Page scope |
RequestScope |
java.util.Map |
Get the value corresponding to the attribute name of the Request scope |
sessionScope |
java.util.Map |
Get the value corresponding to the attribute name of the session scope |
applicationScope |
java.util.Map |
Get the value corresponding to the property name of the Application scope |
param |
java.util.Map |
Like ServletRequest.getParameter(String name). Returns a value of type String |
paramValues |
java.util.Map |
Like ServletRequest.getParameterValues(String name). Returns a value of type String[] |
header |
java.util.Map |
Like ServletRequest.getHeader(String name). Returns a value of type String |
headerValues |
java.util.Map |
Like ServletRequest.getHeaders(String name). Returns a value of type String[] |
cookie |
java.util.Map |
如同HttpServletRequest.getCookies() |
initParam |
java.util.Map |
Like ServletContext.getInitParameter(String name). Returns a value of type String |
The order in which EL parses variables
1. Check for implicit variables
2. If it is not an implicit variable, EL checks whether there is a qualified variable name in the order of pageContext.getAttribute(), HttpServletRequest.getAttribute(), HttpSession.getAttribute(), ServletContext.getAttribute()
3. If it is still not found, an error will be reported
access to collections
Map, there is a map reference
${map.username} 或 ${map["username"]}
List, with a list reference
${list[0]}, note that ${list.0} is wrong
Stream operations can also be used in EL as long as the EL variable is a Java array or Collection instance, but this is not recommended in JSP