Java Web Learning ---- EL expressions

Source: Internet
Author: User
Tags tld

We mentioned an EL expression in previous articles. What exactly is this EL expression? Why is it so useful? Let's take a look at the relevant content of EL expressions.


Introduction to EL expressions:

The full name of EL is Expression Language. EL:
Get data:
EL expressions are mainly used to replace the script expressions on JSP pages to retrieve java objects and obtain data from various types of web domains. (An object in a web domain accesses attributes of javabean, access list sets, Access map sets, and access arrays)
Run the operation:
The EL expression can be used to perform some basic relational operations, logical operations, and arithmetic operations on the JSP page to complete some simple logical operations on the JSP page. $ {User = null}
Obtain common web development objects
EL expressions define some implicit objects. With these implicit objects, web developers can easily obtain references to common web objects to obtain data in these objects.
Call the Java method
EL expressions allow users to develop custom EL functions to call Java class methods through EL expressions on JSP pages.


Use the EL expression to obtain data:

Use the EL expression to obtain the data Syntax: "$ {identifier }"
When an EL expression statement is executed, pageContext is called. the findAttribute method uses the identifier as the keyword to search for the corresponding object from the four domains of page, request, session, and application. If it is found, the corresponding object is returned. If it is not found, the corresponding object is returned. "(note, is not null, but a null String ).
Example: $ {user}

EL expressions can also easily obtain attributes of JavaBean or data of arrays, collections, and Map types. For example:
$ {User. address. city}: gets the city attribute value of the address object in the user object.
$ {User. list [0]}: access an element at a certain position in an ordered set
$ {Map. key}: obtains the value of the specified key in the map set.
Combined with the JSTL foreach tag (as mentioned later), you can use EL expressions to easily iterate arrays or collections of various types. For example:
Iterative Array
Collection of iterative collection types
Iterative map type set


Execute operations in EL expressions

Syntax: $ {operational expression}. EL expressions support the following operators:


Empty OPERATOR: checks whether the variable is null or "null"
Binary Expression: $ {user! = Null? User. name: ""}


Obtain objects in Web Development

11 implicit objects are defined in EL Expression Language. Using these implicit objects, you can easily obtain some common objects in web development and read the data of these objects.
Syntax: $ {implicit object name}: Get Object Reference


Test each implicit object
Notes
When testing headerValues, if the header contains "-", for example, Accept-Encoding, headerValues ["Accept-Encoding"]
For example, $ {cookie. key} is the cookie object. For example, $ {cookie. key. name} or $ {cookie. key. value}


The EL expression uses the Java method.

The EL expression syntax allows developers to develop custom functions to call Java class methods. Example: $ {prefix: method (params )}
Only static methods of Java classes can be called in EL expressions.
The static method of this Java class must be described in the TLD file before it can be called by EL expressions.
EL user-defined functions are used to extend the EL expression function, so that EL expressions can complete the functions that can be done by common Java code.

In general, the development and application of EL udfs include the following three steps:
Compile a static Java method
Write a tag library Descriptor (tld) file to describe user-defined functions in the tld file.
Import and use user-defined functions on the JSP page

Example: Develop el function for escaping html tags

After compiling the tag library description file, you need to place it \ Any subdirectory in the WEB-INF directory or in the WEB-INF directory except the classes and lib directories.
In the TLD File The element specifies the URI of the TLD file. In the JSP file, you need to use this URI to introduce the tag library description file.
Element is used to describe an EL User-Defined Function, where:
The sub-element is used to specify the name of the EL custom function.
The child element is used to specify the complete Java class name,
The child element is used to specify the signature of the static method in the Java class. The method signature must specify the type of the return value of the method and the type of each parameter. Each parameter is separated by a comma.

The specific cases are described in detail when we introduce JSTL.
EL expressions are a technology in JSP 2.0 specifications. Therefore, to correctly parse EL expressions, you must use a WEB server that supports Servlet2.4/JSP2.0 technology.
NOTE: If some Tomcat servers do not support EL expressions
(1) upgrade to tomcat6
(2) Add <% @ page isELIgnored = "false" %> to JSP.

The reserved keyword in EL indicates that when a variable is named, the above name should be avoided to avoid errors during program compilation.


Since EL expressions in JSTL often need to process the displayed strings when displaying data on JSP pages, SUN defines a set of EL function libraries for developers to use for some common processing.
These EL functions are described in the JSTL Development Kit. Therefore, to use the sun el function library on the JSP page, you must import the JSTL Development Kit and import the EL function library to the page, as shown below:
Use the EL function defined by JSTL on the page:
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "fn" %>

Let's take a look at the JSTL function library: 1. fn: toLowerCase
Fn: The toLowerCase function converts all characters contained in a string to lowercase and returns the converted string. It receives a string type parameter, for example
Fn: toLowerCase ("Www.IT315.org") returns the string "www.it315.org"
Fn: toLowerCase ("") returns an empty string

2. fn: toUpperCase
Fn: The toUpperCase function converts all the characters contained in a string to uppercase, and returns the converted string, which receives a string type parameter. For example:
Fn: the return value of toUpperCase ("Www.IT315.org") is the string "WWW. IT315.ORG"
Fn: toUpperCase ("") returns an empty string

3. fn: trim

Fn: the trim function deletes spaces at the beginning and end of a string and returns the result string after the space is deleted. It receives a string-type parameter. Note that,

Fn: the trim function cannot delete spaces in the middle of a string.
For example, fn: trim ("www.it315.org") returns the string "www. it 315.org ".


4. fn: length

The fn: length function returns the size of a set or array, or the number of characters contained in a string. the return value is of the int type. The fn: length function receives a parameter, which can be The items attribute of a tag supports any type, including arrays of any type and java. util. collection, java. util. iterator, java. util. enumeration, java. util. instance objects and strings of Map and other classes.
If the fn: length parameter is null or a set or array object with 0 elements, the function returns 0. If the parameter is a null string, the function returns 0.


5. fn: split

Fn: The split function uses a specified string as the separator to split a string into a string array and return this string array.
Fn: The split function receives two string-type parameters. The first parameter indicates the string to be split, and the second parameter indicates the string used as the separator.
For example, fn: split ("www.it315.org", ".") [1] returns the string "it315 ".


6. fn: join

Fn: The join function uses a string as the separator to combine all elements in a string array into a string and return the merged result string. Fn: The join function receives two parameters. The first parameter is the string array to be operated, and the second parameter is the string used as the separator.
If the second parameter of the fn: join function is a null string, the return value of the fn: join function is directly connected to the element. For example:
Assume that stringArray is an attribute stored in the Web domain, which represents an array of strings with the value {"www", "it315", "org"}, then fn: join (stringArray, ". ") returns the string" www.it315.org"
Fn: join (fn: split ("www, it315, org", ","), ".") returns the string "www.it315.org"


7. fn: indexOf

The fn: indexOf function returns the index value of the specified string that appears for the first time in a string. the return value is of the int type. The fn: indexOf function receives two string-type parameters. If the first parameter string contains the second parameter string, no matter how many times the second parameter string appears in the first parameter string, the fn: indexOf function always returns the index value that appears for the first time. If the first parameter does not contain the second parameter, the fn: indexOf function returns-1. If the second parameter is a null string, the fn: indexOf function always returns 0. For example:
Fn: indexOf ("www.it315.org", "t3") returns 5


8. fn: contains

Fn: The contains function checks whether a string contains a specified string and returns a Boolean value. Fn: The contains function is case sensitive when comparing two strings for equality.
The fn: contains function receives two string-type parameters. If the first parameter string contains the second parameter string, the fn: contains function returns true; otherwise, the function returns false. If the value of the second parameter is a null string, the fn: contains function always returns true. In fact, fn: contains (string, substring) is equivalent to fn: indexOf (string, substring )! =-1.
Ignore the EL function of the Size: fn: containsIgnoreCase


9. fn: startsWith

Fn: The startsWith function is used to check whether a string starts with a specified string and returns a Boolean value.
Fn: The startsWith function receives two string-type parameters. If the first parameter string starts with the second parameter string, the function returns true; otherwise, the function returns false. If the second parameter is a null string, the fn: startsWith function always returns true. For example:
Fn: startsWith ("www.it315.org", "it315") returns false
Corresponding EL function: fn: endsWith


10. fn: replace

Fn: replace function replaces the specified sub-string contained in a string with another specified string and returns the replaced result string. Fn: The replace method receives three string-type parameters. The first parameter indicates the source string to be operated, and the second parameter indicates the substring to be replaced in the source string, the third parameter indicates the string to be replaced. For example:
Fn: replace ("www it315 org", "", ".") returns the string "www.it315.org"

11. fn: substring

Fn: The substring function is used to intercept a substring and return the substring. Fn: The substring function receives three parameters. The first parameter is used to specify the source string to be operated, and the second parameter is used to specify the index value starting from the substring, the third parameter is used to specify the index value after the substring is intercepted. Both the second parameter and the third parameter are of the int type, and the value starts from 0. For example:
Fn: Return Value of substring ("www.it315.org", 4, 9) is the string "it315"


12. fn: substringAfter

Fn: The substringAfter function is used to intercept and return the substring after the first occurrence of the specified substring in a string. Fn: The substringAfter function receives two string-type parameters. The first parameter indicates the source string to be operated, and the second parameter indicates the specified substring. For example:
Fn: substringAfter ("www.it315.org", ".") returns the string "it315.org ".
The corresponding EL function is: fn: substringBefore.

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.