JSP details-EL expressions (1)
EL expression Overview
Before the emergence of EL expressions, when developing Java Web applications, a large number of Java code snippets are often embedded into JSP pages, which makes the pages look messy, while EL expressions are relatively simple.
Example:
The username variable stored in the session is displayed on the page and output to the page. The Code is as follows:
<%
If (session. getAttribute ("username ")! = Null ){
Out. print (session. getAttribute ("username"). toString ();
}
%>
If EL is used, you only need one code to implement it.
$ {Username}
Therefore, EL is commonly used in Web development and is usually used with JSTL.
1. Basic EL syntax
EL expression syntax: It starts with $ {and ends with}. It is a valid expression in the middle. The syntax format is as follows:
$ {Expression}
Parameter description:
Expression: specifies the content to be output. It can be a string or an Expression composed of EL operators.
PS:
The syntax of the EL expression starts with $ {, so if you want to display the string S {on the JSP page, you must add it before. That is, "$ {". Or write "$ {'}".
Example:
Output the "JSP learning" string on the page. The Code is as follows:
$ {'Jsp learn '}
2. EL features
In addition to its simple syntax and ease of use, EL has the following features:
A. EL can be used in combination with JSTL or JavaScript statements.
B. type conversion is automatically performed in EL. If you want to input the sum of two string values (such as n1 and n2) through EL, you can directly connect through "+" (for example, ${n1 + n2 })
C. EL can access not only general variables, but also attributes and nested attributes and set objects in JavaBean.
D. You can perform arithmetic, logical, relational, and conditional operations in EL.
E. You can obtain the namespace (PageContext object, which is the largest inherited object of all other built-in objects on the page. Through this object, you can access other built-in objects)
F. You can access the JSP scope (request, session, application, page) in EL)
G. When using EL for Division calculation, if 0 is used as the divisor, Infinity is returned instead of an error.
H. Extension functions can be mapped to static methods of Java classes.
Compatible with earlier versions-Disable EL
As long as the installed Web server supports Servlet2.4/JSP2.0, EL can be used on the JSP page. Since no EL exists in versions earlier than JSP2.0, JSP also provides EL disabling to be compatible with previous specifications. JSP provides 3 methods to disable EL, which are described as follows.
1. Use the slash ""
Using a slash is a simple method to disable EL. This method only needs to add "" Before the start mark "$ {" of EL. The syntax format is as follows:
$ {Expression}
PS:
This syntax format only uses one or more EL expressions on the disabled page.
2. Use the page command
You can use the JSP page command to disable EL expressions. The syntax format is as follows:
<% @ Page isELIgnored = "Boolean value" %>
Parameter description:
IsELIgnored: used to specify whether to disable the EL of a page. If the attribute value is true, the EL in the page is ignored. Otherwise, the EL in the page will be parsed.
Example:
<% @ Page isELIgnored = "true" %>
PS:
This method is applicable to disabling EL in a JSP page.
3. Configure in web. xml Element
Configure in web. xml You can disable the EL in the server.
Example:
*. Jsp
True
PS:
This method is used to disable EL in all JSP pages of a Web application.
Reserved keywords
Like Java, EL also has its own reserved keywords. when naming variables, these keywords should be avoided, including the use of EL to output variables that have been saved within the scope, and keywords cannot be used, if it has been defined, you need to change it to another variable name. The reserved keywords in EL are as follows:
And |
Eq |
Gt |
Instanceof |
Div |
Or |
Le |
False |
Empty |
Not |
Lt |
Ge |
If elasticsearch uses reserved keywords, an error message is displayed in Eclipse. If you ignore this prompt and directly run the program, an error will occur.
EL operator and priority
EL can access data operators, Arithmetic Operators, Relational operators, conditional operators, and empty operators. The priority of each operator is as follows:
The priority of operators determines the job-seeking sequence of operators when multiple operators exist simultaneously. Operators of the same level are calculated from left to right.
PS:
You can use () to change the priority. Using parentheses in complex expressions makes expressions easier to read and avoid errors.
1. Access data through EL
The "[]" and "," operators provided by EL can be used to access data. In general, the "[]" and "." operations are equivalent and can be replaced by each other.
Example:
You can access the id attribute of userInfo in JavaBean in the following two forms:
$ {UserInfo. id}
$ {UserInfo [id]}
But not all cases can be replaced with each other. For example, when the object's property name contains some special symbols (-or .) you can only use the [] operator to access the attributes of an object. For example, $ {userInfo [user-id]} is correct, while $ {userInfo. user-name} is incorrect. In addition, EL's "[]" operator is used to obtain data in arrays or lists.
Array Element Acquisition:
The "[]" operator can be used to obtain the specified elements of the array, but "." cannot be used.
Example:
Obtain the first element in the array arrBook in the request range. You can use the following EL expression:
$ {ArrBook [0]}
Example:
Compile array. jsp. The file contains three elements, which are used as an array and assigned values. Then, the elements in the array are output through the for loop and EL.
<% @ Page language = java import = java. util. * pageEncoding = UTF-8 %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () +: // + request. getServerName () +: + request. getServerPort () + path + /;
%>
>
<%
String [] arr = {Java, JSP, MySQL}; // defines a one-dimensional array
Request. setAttribute (book, arr); // Save the array in the request object
%>
<%
String [] arr1 = (String []) request. getAttribute (book); // gets the object stored in the request range.
// Outputs the content of a one-dimensional array through loops and EL
For (int I = 0; I <arr1.length; I ++ ){
Request. setAttribute (requestI, I); // Save the cyclic variable I in the variable within the request range
%>
$ {RequestI }:: {book [requestI]}
<%
}
%>
Getting List set Elements
The "[]" operator can also be used to obtain the specified Element in the List set, but the "." operator cannot be used.
Example:
The session stores a List containing three elements and outputs the List using EL.
<% @ Page language = java import = java. util. * pageEncoding = UTF-8 %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () +: // + request. getServerName () +: + request. getServerPort () + path + /;
%>
>
<%
List List = new ArrayList (); // declare a List set object
List. add (biscuit );
List. add (milk );
List. add (bread );
Session. setAttribute (goodsList, list); // Save the List to the session.
%>
<%
List List1 = (List ) Session. getAttribute (goodsList); // gets the object stored in the session
For (int I = 0; I <list1.size (); I ++ ){
Session. setAttribute (sessionI, I );
%>
$ {SessionI }:: {goodsList [sessionI]}
<%
}
%>
2. Check whether the object is empty in EL.
In EL, you can use the empty operator to determine whether an object is null. This operator is a prefix operator, that is, the empty operator is located in front of the operand, determines whether an object is null or empty. The format of the Empty operator is as follows:
$ {Empty. expression}
Example:
Define the user and user1 variables in the two request ranges. The values are null and "".
<% Request. setAttribute ("user", ""); %>
<% Request. setAttribute ("user1", null); %>
Then, use the empty operator to determine whether user and user1 are empty.
$ {Empty user} // return true
$ {Empty user1} // return true
In addition, the empty operator can be used with the not operator to determine whether an object or variable is null.
Example:
<% Request. setAttribute ("user ","");
$ {Not empty user} // The returned value is false.
3. Logical Relationship calculation in EL
In EL, logical Relational operators can be used to perform logical relational operations. Relational operators are used to compare two expressions. The expression for comparison can be numeric or string. Logical operators are commonly used to operate data of the Boolean type. Logical operators and Relational operators are often used together.
Example:
When judging the test score, you can use the following expression to determine the score between 60 and 80.
Score> 60 and score <80
Relational operators
In EL, a relational operator in 6 is provided. The format of Relational operators is as follows:
Operator |
Function |
Example |
= Or eq |
Equal |
${10 = 10}/$ {10 eq 10} |
! = Or ne |
Not equal |
$ {10! = 10}/$ {"A" ne ""} |
<Or lt |
Less |
${7 <6}/$ {"A" lt "B "} |
> Or gt |
Greater |
$ {7> 6}/$ {"A" gt "B "} |
<= Or le |
Less than or equal |
$ {"A" <= ""} |
> = Or ge |
Greater than or equal |
${7 >=6}/$ {7 ge 6} |
Logical operators
Conditional operations in EL
Syntax format:
$ {Condition expression? Expression 1: expression 2}
Parameter description:
Conditional expression: used to specify a conditional expression. The value of this expression is Boolean. It can be composed of Relational operators, logical operators, and empty operators.
Expression 1: used to specify the value to be returned when the value of the conditional expression is true.
Expression 2: used to specify the value to be returned when the conditional expression is false.
Example: When the cart value of the variable is empty, the cart value is output. Otherwise, the cart value is output.
$ {Empty cart? "Cart is blank": cart}
In general, condition operators can use condition labels in JSTL. Or Replace