Java El Expression

Source: Internet
Author: User

EL full name is expression Language

EL syntax is simple, its biggest feature is the use of very convenient. Next, the main syntax structure of EL is introduced:

${sessionscope.user.sex}

All El are beginning with ${and ending with}. The El example above means: from the scope of the session, obtain

The gender of the user. If you follow the previous JSP scriptlet the following wording:

User user = (user) Session.getattribute ("User");

String sex = User.getsex ();

Compared with the comparison, El's syntax can be found to be more convenient and concise than the traditional JSP scriptlet.

. with the [] operator

EL provides. and [] Two operators to navigate the data. The following two represent the same meaning:

${sessionscope.user.sex} equals ${sessionscope.user["Sex"]}

And [] can also be mixed at the same time, as follows:

${sessionscope.shoppingcart[0].price}

The return result is the price of the first item in ShoppingCart.

However, there are two situations in which the two are different:

(1) When the name of the property to be accessed contains some special characters, such as. or – such as symbols that are not letters or numbers, you must use [], for example: ${user. My-name}

The above is not the correct way, should be replaced by: ${user["My-name"]}

(2) We will consider the following situations:

${sessionscope.user[data]}

At this point, data is a variable, if the value of data is "sex", then the above example equals ${sessionscope.user.sex};

If the value of data is "name", it is equal to ${sessionscope.user.name}. Therefore, if you want to dynamically fetch values, you can do so using the above method, but . cannot be dynamically evaluated.

EL variables

el  access variable data is simple, for example: ${username}. It means to take out a variable whose name is username in a range. Because we do not specify a range of username, so its default value will be first from the page  range, if not found, then in the request, Session, application range. If the way to find username, directly back, no longer continue to find, but if all the scope is not found, the return null, of course, El Expression will also be optimized, the page is displayed blank, instead of printing output null.

Attribute range (jstl name)

Name in El

Page

Pagescope

Request

Requestscope

Session

Sessionscope

Application

Applicationscope

We can also specify which range of variables to remove:

example

description

${pagescope.username}

Remove the username variable of page range

${requestscope.username}

Remove the username variable of the request range

${ Sessionscope.username}

remove username variable of Session range

${applicationscope.username}

Application variable of username range

Among them, Pagescope, Requestscope, Sessionscope and Applicationscope are all hidden objects of El, by their names it is easy to guess what they mean, for example: ${ Sessionscope.username} is the username variable that takes out the session range. This is not the same as the previous JSP's notation:

String username = (string) session.getattribute ("username"), easy and concise.

Automatic transition type

In addition to providing a convenient syntax for accessing variables, EL has another handy feature: Automatic transition types, let's look at the following example:

${param.count + 20}

If the form has a value of Count 10 o'clock, the result above is 30. Readers who have not touched the JSP before may think that the above example is taken for granted, but not in JSP 1.2, because the values from the form are all string, so when you receive it, you must convert it to another type, such as int, float, and so on. Before you can perform some mathematical operations, here's how it's done:

String Str_count = Request.getparameter ("Count");

int count = Integer.parseint (Str_count);

Count = count + 20;
So, be careful not to confuse the syntax of Java (when strings and numbers convert numbers to strings with a "+" link) .


EL
Hidden Objects

The JSP has 9 hidden objects, and El also has its own hidden object. El Hidden Objects Total 11

Implied object

Type

Description

PageContext

Javax.servlet.ServletContext

Represents the PageContext of this JSP

Pagescope

Java.util.Map

Gets the value that corresponds to the property name of the page range

Requestscope

Java.util.Map

Gets the value corresponding to the property name of the request scope

Sessionscope

Java.util.Map

Gets the value corresponding to the property name of the session scope

Applicationscope

Java.util.Map

Gets the value that corresponds to the property name of the application range

Param

Java.util.Map

Like Servletrequest.getparameter (String name). Callback value of String type

Paramvalues

Java.util.Map

Like Servletrequest.getparametervalues (String name). Callback string[] Type value

Header

Java.util.Map

Like Servletrequest.getheader (String name). Callback value of String type

Headervalues

Java.util.Map

Like Servletrequest.getheaders (String name). Callback string[] Type value

Cookies

Java.util.Map

Like Httpservletrequest.getcookies ()

Initparam

Java.util.Map

Like Servletcontext.getinitparameter (String name). Callback value of String type

One thing to note, though, is that if you are going to use the El to output a constant, the string will be double-quoted, otherwise the El will default to you as a variable to deal with the constant, if the variable in the 4 Declaration range does not exist, the output is null, if present, the value of the variable is output.

Properties (Attribute) and scope (scope)

The El implied objects that are related to a range contain the following four: Pagescope, Requestscope, Sessionscope, and

Applicationscope, they are basically the same as JSP's PageContext, request, session, and application, so I'll just slightly explain here. It should be noted, however, that these four hidden objects can only be used to obtain the range attribute value, which is the getattribute (String name) in the JSP, but cannot obtain other relevant information, for example: The Request object in the JSP can be accessed in addition to the property. You can also obtain the user's request parameters or header information, and so on. However, in El, it can only be used to obtain the corresponding range of attribute values, for example: we want to store a property in the session, its name is username, in the JSP use Session.getattribute ("username") To get the value of the username, but in El, it uses ${sessionscope.username} to get its value.

Cookies

The so-called cookie is a small text file, it is a key, value in the way the session tracking content in this text file, this text file usually exists in the browser's staging area. JSTL does not provide a cookie-setting action, because this action is usually something that the backend developer has to do, rather than handing it over to the front-end developer. If we set a value in the cookie named Usercountry, then we can use ${cookie.usercountry} to get it.

Headers and Headervalues

The header stores the data that the user's browser and the server use to communicate, and when the user requests the Web page of the server, it sends out a header file that records the requested information, such as the version of the user's browser, the area set by the user's computer, and other relevant data. If you want to get the version of the user's browser, that is ${header["user-agent"}. In addition, under rare opportunities, it is possible to have different values for the same header name, and you must instead use Headervalues to obtain these values.

Note: Because user-agent contains "-" This special character, you must use "[]" instead of

$ (header. user-agent).


Initparam

Just like other properties, we can set our own web site environment parameters (context), when we want to get these parameters initparam like other properties, we can set the Web site environment parameters (context), when we want to get these parameters

<?xml version= "1.0" encoding= "Iso-8859-1"?>

<web-app xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

version= "2.4";:

<context-param>

<param-name>userid</param-name>

<param-value>mike</param-value>

</context-param>:

</web-app>

Then we can use ${initparam.userid} directly to get the name UserID, whose value is Mike's argument. Here is the previous procedure: String userid = (string) application.getinitparameter ("userid");

Param and Paramvalues
You typically use the following method when you get user parameters:

Request.getparameter (String name)
Request.getparametervalues (String name)

In El, you can use both Param and paramvalues to get the data.
${param.name}
${paramvalues.name}

Here param functions are the same as Request.getparameter (String name), while Paramvalues and
The Request.getparametervalues (String name) is the same. If the user fills out a table with a table name of username, then we can use ${param.username} to get the values that the user fills in.

See here, we should be very clear that the El expression can only be used by the built-in object value, that is, read-only operation, if you want to write operations let the background code to complete, after all, the El expression is only the output label on the view.

PageContext

We can use ${pagecontext} to get additional details about user requirements or pages. The following table lists some of the more common parts

Expression

Description

${pagecontext.request.querystring}

Gets the requested argument string

${pagecontext.request.requesturl}

Gets the requested URL, but does not include the request's argument string, which is the HTTP address of the servlet.

${pagecontext.request.contextpath}

The name of the webapplication of the service

${pagecontext.request.method}

Method of getting HTTP (get, POST)

${pagecontext.request.protocol}

Access to the protocol used (http/1.1, http/1.0)

${pagecontext.request.remoteuser}

Get user Name

${PAGECONTEXT.REQUEST.REMOTEADDR}

Get the IP address of the user

${pagecontext.session.new}

Determine if the session is a new, so-called new session, indicating that the client has not yet been created by the server

${pagecontext.session.id}

Get the ID of the session

${pagecontext.servletcontext.serverinfo}

Access to host-side service information

This object can effectively improve the code hard coding problem, such as a page with a tag link to access a servlet, if the HTTP address of the servlet is written dead if the servlet's servlet-mapping changes must be to modify the source code, This will greatly compromise the maintainability.

EL arithmetic arithmetic

Expression language supports many arithmetic and logical operators, all arithmetic operators supported in the Java language, expression languages can be used, and even some arithmetic and logical operators not supported by the Java language, and expression languages are supported.

Code

The above page demonstrates the functions of the addition, subtraction, multiplication, divide, and remainder arithmetic operators supported by the expression language, and the reader may also find that the expression language also supports the operators such as Div, mod, and so on. Moreover, the expression language treats all values as floating-point numbers, so the essence of 3/0 is 3.0/0.0, and the result should be infinity.

If you need to output the "$" symbol normally in a page that supports expression language, the "$" symbol is preceded by the escape character "\", or the system thinks "$" is a special token of the expression language.

EL Relational Operators

Relational operators

Description

Example

Results

= = or EQ

Equals

${5==5} or ${5eq5}

True

! = or NE

Not equal to

${5!=5} or ${5ne5}

False

< or LT

Less than

${3<5} or ${3LT5}

True

> or GT

Greater than

${3>5} or {3GT5}

False

<= or Le

Less than or equal

${3<=5} or ${3le5}

True

>= or GE

Greater than or equal

5} or ${3ge5}

False

Expression languages can be compared not only between numbers and numbers, but also between characters and characters, and comparisons of strings are sized according to their corresponding Unicode values.

Note: When using the El relational operator, it is not possible to write:
${param.password1} = = ${param.password2}
Or
${${param.password1} = = ${Param.password2}}
and should be written
${Param.password1 = = Param.password2}


EL
logical Operators

example

Result

&& or and

intersection ${a && B} or ${a and B}

true/ False

| | or

true/false

! or not

non ${! A} or ${not a}

true/false

Empty operator

The empty operator is used primarily to determine whether a value is empty (null, an empty string, an empty collection).

Conditional operators

${A? B:C}

Java El Expression

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.