Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use EL expression

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use EL expressions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

EL (Expression Language) expression language

It is one of the formal standard specifications of JSP2.0.

To run EL, you must first find a java web container that supports JSP2.0

First, let's test whether your java Web container supports JSP2.0.

Here, put this JSP in your hungry web container.

Index.jsp

-

Servlet supports:.

-

To visit this page, if the supported version of servlet is 2.4 or above, then your web container should be able to support JSP2.0.

For example:

${user.firstName}

EL reserved word

And Or No Instanceof

Eq ne le ge

Lt gt empty null

True false div mod

EL reserved identifier

PageContext pageScope requestScope sessionScope

ApplicationSocpe param paramValues header

HeaderValues cookie initParam

Scope (4)

Page request session application

Any identifier that appears in an EL expression but does not correspond to any EL implicit object is automatically assumed to refer to an object stored in a JSP scope.

EL checks the four scopes of page, request, session, and application in turn.

As soon as the corresponding value is found, the value is returned immediately, and the search is not continued.

If it is not found after traversal, return null

EL accessor

Point (.) Operator is usually used to access the properties of an object

Example:

${user.sex}

${user.birthday.year}

The square bracket ([]) operator is commonly used to retrieve the elements of an uncle or collection.

Example:

${email [1]} Note that the index starts at'0'

If the retrieved object is a collection that implements the Map interface, the square bracket operation symbol uses the key of inertia to retrieve the value stored in the diffraction

Example:

${employee {"id"}}

-

-- 11 implicit objects of EL

PageContext

PageScope requestScope sessionScope applicationScope

Param paramValues

Header headerValues

Cookie

InitParam

-

Access address: http://www.wangyudong.com:8080/elapp/index.jsp?id=99#123456789

Test results:

Servlet support: version 2.4

${pageContext.servletContext.serverInfo}: Apache Tomcat/5.0.28

${pageContext.request.requestURL}: http://www.wangyudong.com:8080/elapp/index.jsp

${pageContext.request.requestURI}: / elapp/index.jsp

${pageContext.request.contextPath}: / elapp

${pageContext.request.queryString}: id=99

${pageContext.request.method}: GET

${pageContext.request.protocol}: HTTP/1.1

${pageContext.request.localAddr}: 127.0.0.1

${pageContext.session.new}: false

${pageContext.request.requestedSessionId}: A9D19BB7273C2C6622E8C8F2174215D7

${pageContext.session.id}: A9D19BB7273C2C6622E8C8F2174215D7

-

The four scopes of EL:

Page scope pageScope

Example:

${pageScope.userName}

${pageScope.password}

Request scope requestScope

Example:

${requestScope.userName}

${requestScope.password}

Session scope sessionScope

Example:

${sessionScope.userName}

${sessionScope.password}

Application scope applicationScope

${applicationScope.userName}

${applicationScope.password}

Param object, request parameter object

Store the request parameters sent by the customer

UserName: ${param.userName}

Password: ${param.password}

The paramValues object, the Map mapping object, stores the key-value pairs of the 'name' and 'value' of all request parameters.

It can be said to create a Map object for each parameter with a different name.

UserName: ${paramValues.userName [0]}

UserName: ${paramValues.userName [1]}

Header object: the object that stores the header information

Example:

${header ["User-Agent"]} / / get the user's browser information

Cookie object: Cookie

Map mapping with name-value pairs related to Cookie

Example:

${cookie.JSESSIONID.name}: ${cookie.JSESSIONID.value}

IniParam object: initialization parameter

Configuration in web.xml:

Wyd

Wang yu dong

Get parameters in index.jsp

InitParam value of wyd: ${initParam ["wyd"]}

EL displays text:

${"ssss"}

${'kkkk'}

EL displays numbers:

${123}

${22.9865}

EL displays Boolean values

${true}

${false}

EL arithmetic operation

The arithmetic operator illustrates the example result

+ addition ${123 $456} 579

-subtract ${123-456}-333

* multiplication ${123 456} 56088

/ or div Division ${123 div 456} or ${123 div 456} 0.2697368

% or mod balance ${123% 456} or% {123 mod 456} 123

Relational operation

The relational operator illustrates the sample result

= = or eq equals ${123 eq 456} or ${123 eq 456} false

! = or ne is not equal to ${123 ne 456} or ${123 ne 456} false

< 或 lt 小于 ${123 或 gt 大于 ${123>

456} or ${123 gt 456} false

= 456} or ${123 ge 456} false

Logical operation

Logical operator description example (A=true B=false) result

& & or and logic in ${AggregaB} or ${An and B} false

| or or logic or ${A | | B} or ${An or B} true |

! Or not logic is not ${! a} or ${not A} false

Verification operation

The validation operator illustrates the sample result

${empty null} true

Whether empty is empty ${empty ""} true

${empty "123"} false

EL function

1, write EL tag script class.

Here is the content of the class

Note:

1, the class must be public

2, the method in the class must be public static

_________________

Package wyd.jsp.el.tag

Public class MathFunction {

/ * *

* PI is pi

, /

Private static final double PI=3.1415926

/ * *

*

* @ param radius circle radius

* @ return circumference

, /

Public static double girthOfCircle (double radius) {

Double grith=0

Grith=PI * 2 * radius

Return grith

}

/ * *

*

* @ param radius circumference

* @ return circle area

, /

Public static double areaOfCircle (double radius) {

Double area=0

Area= PI * radius * radius

Return area

}

}

_________________

2, write the tag definition file * .tld file

Here are the contents of the "wyd_math.tld" file

And the path I chose to save the file is "WEB-INF/tlds/wyd_math.tld"

-

1.0

/ http://www.wangyudong.com

Girth

Wyd.jsp.el.tag.MathFunction

Double girthOfCircle (double)

Area

Wyd.jsp.el.tag.MathFunction

Double areaOfCircle (double)

-

3, now configured in web.xml

My configured web.xml file

-

Index.jsp

Http://www.wangyudong.com/tld/math

/ WEB-INF/tlds/wyd_math.tld

-

Explanation:

Taglib-uri: give a random string to mark it, and you can find it when the jsp file applies it to the tag.

Taglib-location: is the location of the description file * .tld file where the tag exists. The beginning of the "/" indicates looking from the root directory of an application 'application'.

4, apply EL custom tags in JSP

My jsp file: "el.jsp"

-

The circumference of the circle of Renew100: ${wyd:girth (100)}

Area of the circle of wyd:area 100: ${circle (100)}

5. The results of the interview are out.

Result:

-

The circumference of the circle of Renew100: 628.31852

The area of the circle of Rust 100: 31415.9260000003

Thank you for reading! This is the end of this article on "how to use EL expressions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report