Introduction to EL expressions is required (recommended). Introduction to el expressions is required.

Source: Internet
Author: User

Introduction to EL expressions is required (recommended). Introduction to el expressions is required.

To make JSP writing easier. The expression language is inspired by ECMAScript and XPath expressions. It provides methods to simplify expressions in JSP.

Jsp el language definition

Expression Language (e l) Objective: To make JSP writing easier.

The expression language is inspired by ECMAScript and XPath expressions. It provides methods to simplify expressions in JSP. It is a simple language based on available namespaces (PageContext attributes), nested attributes, and pairs of sets, operators (arithmetic, relational, and logical) and a set of implicit objects.

EL provides the ability to use runtime expressions outside the scope of JSP scripting elements. A script element is an element on a page that can be used to embed Java code into a JSP file. They are usually used for object operations and computing that affects the generated content. JSP 2.0 adds an EL expression as a scripting element.

I. Introduction to JSP EL

1. syntax structure

$ {Expression}

2. [] and. Operators

EL provides "." and "[]" operators to access data.

When the attribute name to be accessed contains some special characters, such as. Or? If it is not a letter or number, you must use "[]". For example:

$ {User. My-Name} should be changed to $ {user ["My-Name"]}

If you want dynamic values, you can use "[]", but "." cannot achieve dynamic values. For example:

Data in $ {sessionScope. user [data]} is a variable.

3. Variables

EL is easy to access variable data, for example, $ {username }. It means to retrieve the variable named username in a range.

Because we do not specify the username of the range, it will search for the range from Page, Request, Session, and Application in sequence.

If the username is found on the way, it will be directly returned and will not be found any more, but if all the ranges are not found, null will be returned.

Attribute range

Name in EL

Page

Pagination

Request

RequestScope

Session

SessionScope

Application

ApplicationScope

Ii. Valid expressions in JSP EL

A valid expression can contain text, operators, variables (Object references), and function calls. We will understand each of these valid expressions:

1. Text

The JSP Expression Language defines the following words that can be used in an expression:

Text

Text Value

Boolean

True and false

Integer

Similar to Java. Can contain any positive or negative number, such as 24,-45, 567

Floating Point

Similar to Java. Can contain any positive or negative floating point numbers, such as-1.8E-45, 4.567

String

Any string limited by single or double quotation marks. For single quotes, double quotes, and backslash, The backslash character is used as the escape sequence.

Note that if double quotation marks are used at both ends of a string, the single quotation marks do not need to be escaped.

Null

Null

2. Operators

JSP expression language provides the following operators, most of which are commonly used operators in Java:

Terms

Definition

Arithmetic type

+,-(Binary), *,/, div, %, mod,-(one dollar)

Logical type

And, &, or, | ,! , Not

Relational

=, Eq ,! =, Ne, gt, <=, le,> =, ge.

It can be compared with other values or with Boolean, String, integer, or floating-point text.

Null

The null operator is a prefix operation that can be used to determine whether the value is null.

Conditional

A? B: C. Assign values to B or C based on the value of.

3. Implicit object

JSP Expression Language defines a set of implicit objects, many of which are available in JSP scriplet and expressions:

PageContext

The context of the JSP page. It can be used to access JSP implicit objects, such as requests, responses, sessions, outputs, and servletContext.

For example, $ {pageContext. response} assigns a value to the response object of the page.

In addition, it provides several implicit objects that allow simple access to the following objects:

Terms

Definition

Param

Map the request parameter name to a single String parameter value (obtained by calling ServletRequest. getParameter (String name ).

The getParameter (String) method returns a parameter with a specific name. Expression $ (param. name) is equivalent to request. getParameter (name ).

ParamValues

Map the request parameter name to a numeric array (obtained by calling ServletRequest. getParameter (String name ).

It is very similar to a param implicit object, but it retrieves a string array instead of a single value. Expression $ {paramvalues. name) is equivalent to request. getParamterValues (name ).

Header

Map the request header name to a single String header value (obtained by calling ServletRequest. getHeader (String name ).

The expression $ {header. name} is equivalent to request. getHeader (name ).

HeaderValues

Map the request header name to a numeric array (obtained by calling ServletRequest. getHeaders (String ).

It is very similar to the header implicit object. The expression $ {headerValues. name} is equivalent to request. getHeaderValues (name ).

Cookie

Map the cookie name to a single cookie object. Client requests sent to the server can obtain one or more cookies.

Expression $ {cookie. name. value} returns the first cookie value with a specific name. If the request contains multiple cookies with the same name, use the $ {headerValues. name} expression.

InitParam

Map the context initialization parameter name to a single value (obtained by calling ServletContext. getInitparameter (String name ).

In addition to the preceding two types of implicit objects, some objects allow access to variables in various scopes, such as Web context, session, request, and page:

Terms

Definition

Pagination

Map the variable names in the page range to their values.

For example, an EL expression can use $ {pagination. objectName} to access a page range object in a JSP,

You can also use $ {pagination. objectName. attributeName} to access object attributes.

RequestScope

Map the variable name in the request range to its value. This object allows access to the properties of the request object.

For example, the EL expression can use $ {requestScope. objectName} to access an object in the JSP request range,

You can also use $ {requestScope. objectName. attributeName} to access the attributes of an object.

SessionScope

Map the name of the variable in the session range to its value. This object allows access to the properties of the session object.

Example: $ {sessionScope. name}

ApplicationScope

Map the variable names in the application range to their values. This implicit object allows access to objects within the application range.

Iii. Special emphasis:

1. Note that when the expression references these objects based on their names, the corresponding objects instead of the corresponding attributes are returned. For example, even if the existing pageContext attribute contains some other values, $ {pageContext} returns the PageContext object.

2. Note that <% @ page isELIgnored = "true" %> indicates whether EL language is disabled. TRUE indicates that EL language is disabled. FALSE indicates that EL language is disabled by default in JSP2.0.

Iv. Examples

1. For example,

<% = Request. getParameter ("username") %> equivalent to $ {param. username}

2. For example, the following EL language can be completed. If the username is null, null is not displayed, but the value is not displayed.

<% = User. getAddr () %> is equivalent to $ {user. addr }.

3. For example:

<% = Request. getAttribute ("userlist") %> equivalent to $ {requestScope. userlist}

4. For example, the principle is as follows: 3.

$ {SessionScope. userlist} 1

$ {RequestScope. userlist} 2

$ {ApplicationScope. userlist} 3

$ {Pagination. userlist} 4

 

$ {Uselist}: The execution sequence is 4 1 2 3.

"." Is followed by a string, which is not a real built-in object and cannot be called.

5. For example,

<% = User. getAddr () %> equivalent to $ {user. addr}

The user in front of the first sentence is a variable.

The user must be an attribute in a certain range.

The above EL expression entry will be read (recommended) is all the content shared by Alibaba Cloud xiaobian. I hope to give you a reference and support for the customer's house.

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.