Introduction to El expression (recommended)

Time:2020-2-7

In order to makeJSPIt’s easier to write.The inspiration of expression language comes from ECMAScript and XPath Expression language, which provides JSP To simplify the expression.

JSP El language definition

E LExpression Language)Purpose: in order toJSPIt’s easier to write.

The inspiration of expression language comes from ECMAScript and XPath Expression language, which provides JSP To simplify the expression. It’s a simple language based on available namespaces(PageContext Properties), nested properties and accessors to sets, operators (arithmetic, relational, and logical), mapping to Java Class and a set of implicit objects.

EL Provided in JSP The ability to use runtime expressions outside the scope of scripting elements. Scripting elements refer to pages that can be used in JSP Embedded in file Java The element of the code. They are often used for object manipulation and for performing calculations that affect what is generated.JSP 2.0 take EL Expression is added as a scripting element.

1、 Introduction to JSP el

1, grammatical structure

${expression}

2[ ]And.operator

EL provide“.”and“[ ]”Two operators to access data.

When the property name to be accessed contains some special characters, such as.or?It is necessary to use symbols that are not letters or numbers“[ ]”。 For example:

${user. My name} should be changed to ${user [“my name”]}

If you want to take a dynamic value, you can use the“[ ]”To do it, and“.”Dynamic value cannot be obtained. For example:

Data is a variable in ${sessionscope. User [data]}

3Variables

ELThe way to access variable data is simple, for example:${username}。 It means to take out a range namedusernameVariables.

Because we didn’t specify a scopeusername, so it will follow thePageRequestSessionApplicationScope lookup.

If you find it on the wayusername, it will be returned directly, and it will not continue to search, but if the whole range is not found, it will be returnednull

Attribute range

ELName in

Page 

PageScope

Request   

RequestScope

Session

SessionScope

Application

ApplicationScope

2、 Valid expressions in JSP el

Valid expressions can contain text, operators, variables (object references), and function calls. We will look at each of these valid expressions separately:

1Text

JSP Expression language defines the following text that can be used in expressions:

Written words

Literal value

Boolean

true and false

Integer

And Java Similar. Can contain any positive or negative number, for example 24-45567

Floating Point

And Java Similar. You can include any positive or negative floating-point number, such as -1.8E-454.567

String

Any string qualified by single or double quotes. For single quotes, double quotes, and backslashes, use the backslash character as the escape sequence.

It is important to note that single quotes do not need to be escaped if double quotes are used at both ends of the string.

Null

null

2Operator

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

term

Definition

Arithmetic type

+(binary)*/div%mod($1)

Logic type

and&&or||!not

Relational type

==eq!=ne、、gt<=le>=ge

You can compare with other values, or with Boolean, string, integer, or floating-point text.

empty

An empty operator is a prefix operation that can be used to determine whether a value is empty.

Conditional type

A ?B :C。 according to A The result of assignment B or C

3, implicit objects

JSP The expression language defines a set of implicit objects, many of which are JSP scriplet Available in and expressions:

pageContext

JSP The context of the page. It can be used to access JSP Implicit objects, such as request, response, session, outputservletContext And so on.

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

In addition, there are several implicit objects that allow easy access to:

term

Definition

param

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

getParameter (String) Method returns a parameter with a specific name. Expression $(param.name) Amount to request.getParameter (name)

paramValues

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

It and param Implicit objects are very similar, but they retrieve an array of strings instead of a single value. Expression ${paramvalues.name) Amount to request.getParamterValues(name)

header

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

Expression ${header.name} Amount to request.getHeader(name)

headerValues

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

It’s very similar to the head implicit object. Expression ${headerValues.name} Amount to request.getHeaderValues(name)

cookie

take cookie Name mapping to a single cookie Object. One or more client requests to the server can be obtained cookie

Expression ${cookie.name.value} Returns the first with a specific name cookie Value. If the request contains more than one cookie, you should use ${headerValues.name} Expression.

initParam

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

In addition to the two types of implicit objects mentioned above, some objects allow access to a variety of variables, such as Web Context, session, request, page:

term

Definition

pageScope

Map the variable name of the page scope to its value.

For example,EL Expressions can use ${pageScope.objectName} Visit one JSP Page wide objects in,

You can also use ${pageScope.objectName.attributeName} Access the properties of the object.

requestScope

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

For example,EL Expressions can use ${requestScope.objectName} Visit one JSP The object of the request scope,

You can also use ${requestScope.objectName.attributeName} Access the properties of the object.

sessionScope

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

For example:${sessionScope.name} 

applicationScope

Map application wide variable names to their values. The implicit object allows access to application scoped objects.

3、 In particular:

1, note that when an expression references one of these objects by name, it returns the corresponding object instead of the corresponding property. For example: even existing pageContext Property contains some other values,${pageContext}Also returned PageContext Object.

2 Be careful <%@ page isELIgnored=”true” %> Indicates whether to disableELlanguage,TRUEExpress prohibition.FALSEIndicates no prohibition.JSP2.0Enabled by default inELLanguage.

4、 Examples

1For example,

<%=request.getParameter(“username”)%>      Equivalent to       ${param.username}

2, for example, but the following sentenceELLanguage can be done if you get oneusernameIf it is empty, it will not be displayednull,Instead, the value is not displayed.

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

3For example,

<%=request.getAttribute(“userlist”)%>     Equivalent to    ${ requestScope.userlist }

4, for example, the principle is shown in the above example3

${sessionScope.userlist}             1

${requestScope.userlist}             2

${applicationScope.userlist}         

${pageScope.userlist}             4

 

${uselist}: execution order is 4 1 2 3.

After ‘.’, it’s just a string. It’s not a real built-in object. It can’t be called.

5For example,

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

Before the first sentenceuser,Is a variable.

After the second sentenceuser, must be a property in a range.

The above introduction to El expression (recommended) is all the content that Xiaobian shared with you. I hope you can give us a reference, and I hope you can support developepaer more.

Recommended Today

Hot! Front and rear learning routes of GitHub target 144K

Hello, Sifu’s little friend. I’m silent Wang Er. Last week, while appreciating teacher Ruan Yifeng’s science and technology weekly, I found a powerful learning route, which has been marked with 144K on GitHub. It’s very popular. It covers not only the front-end and back-end learning routes, but also the operation and maintenance learning routes. As […]