EL expression in the use of jsp

EL expressions, called Expression Language EL-wide

EL syntax is very simple, it's biggest feature is very convenient for use. Next comes the main grammatical structures EL:

$ (SessionScope.user.sex)

EL are all starting with $ (to) for the end. EL means the above example: the range from the Session to obtain

The user's gender. If according to the wording before the JSP Scriptlet as follows:

User user = (User) session.getAttribute ("user");

String sex = user.getSex ();

Comparison of the two below can be found in EL syntax is more convenient than the traditional JSP Scriptlet, simple.

. With the [] operator

EL provide. And [] of two operators to navigate the data. The following represents the meaning of both is the same:

$ (SessionScope.user.sex) is equal to $ (sessionScope.user ["sex"])

. And [] can be also mixed, as follows:

$ (SessionScope.shoppingCart [0]. Price)

Return result shoppingCart the first item in the price.

However, the following two conditions are different between the two:

(1) When you want to access the property name contains special characters, such as. Or - such as letters or numbers are not symbols, they have to use [], for example: $ (user.My-Name)

These are not the right way, it should be changed to: $ (user ["My-Name"])

(2) We consider the following:

$ (SessionScope.user [data])

At this point, data is a variable, if the data value of "sex" when it is equal to the above example of $ (sessionScope.user.sex);

If the data value of "name" when it is equal to $ (sessionScope.user.name). Therefore, if the dynamic value, it is possible to do with the above method, but. Unable to do dynamic values.

EL Variables

EL access the variable data is simple, for example: $ (username). It means that out of a range variable named username. Because we did not specify which of a range of username, so it will start with the default search Page scope, if not found, then sequentially to the Request, Session, Application scope. If the way to find the username, the direct return, not to look down, but if all of the range are found, would return null, of course, will make the optimization of EL expression, a blank page instead of printed output NULL.

Property range (jstl name)
EL in the name of the

Page
PageScope

Request
RequestScope

Session
SessionScope

Application
ApplicationScope

We can also specify which of a range of variables out:

Sample Description

$ (PageScope.username)
Page out the scope of username variable

$ (RequestScope.username)
Request username out the scope of the variable

$ (SessionScope.username)
Remove the Session variable username range

$ (ApplicationScope.username)
Remove the Application scope variable username

Which, pageScope, requestScope, sessionScope and applicationScope are implicit EL objects by their name can easily guess the meaning they represent, for example: $ (sessionScope.username) is the Session scope out the username variable. The wording is not the wording than the previous JSP:

String username = (String) session.getAttribute ("username"); easy, simple and many.

Automatic shift type

EL addition to providing convenient access to variables outside the syntax, which is another handy feature: automatically change the type, we look at this example below:

$ (Param.count + 20)

Came the count value if the form 10, then the results above 30. JSP not contacted before the reader might think that the example above is a matter of course, but in JSP 1.2 being unable to do so, because the value coming from the form that they are invariably the type String, so when you receive after , then it must be converted to other types, such as: int, float, etc., and then to perform some mathematical operations, the following is the practice before:

String str_count = request.getParameter ("count");

int count = Integer.parseInt (str_count);

count = count + 20;
So, be careful not to and java syntax (when strings and numbers with "+" link will convert a number string), confused woven.

EL implicit objects

9 of JSP implicit object, which has its own implicit EL objects. EL implicit object a total of 11

Hidden object type description

PageContext
javax.servlet.ServletContext
That the JSP's PageContext

PageScope
java.util.Map
Page range to obtain the corresponding value of the property name

RequestScope
java.util.Map
Request scope attribute name to obtain the corresponding value of

sessionScope
java.util.Map
Session scope attribute names to obtain the corresponding value of

applicationScope
java.util.Map
Application scope attribute names to obtain the corresponding value of

param
java.util.Map
As ServletRequest.getParameter (String name). Return value of type String

paramValues
java.util.Map
As ServletRequest.getParameterValues (String name). Return String [] type value

header
java.util.Map
As ServletRequest.getHeader (String name). Return value of type String

headerValues
java.util.Map
As ServletRequest.getHeaders (String name). Return String [] type value

cookie
java.util.Map
As HttpServletRequest.getCookies ()

initParam
java.util.Map
As ServletContext.getInitParameter (String name). Return value of type String

But there is one thing to note is that if you use the words of EL output a constant string to double quotes, otherwise EL will default to a constant as you think of a variable, then if the variable is declared in the four areas does not exist, will output empty, if the output value of the variable.

Properties (Attribute) and scope (Scope)

And scope of the EL implicit object contains the following four: pageScope, requestScope, sessionScope and

applicationScope, and JSP are essentially the pageContext, request, session and application as well, so I note here only a little bit. Although it must be noted that the four implicit object attribute values can only be used to get range, that JSP in getAttribute (String name), but can not access other relevant information, such as: JSP request object in addition to access to property but also can obtain a user's request parameters or header information, etc.. But in the EL in, it can simply be used to obtain the corresponding range of property values, for example: we want to store an attribute in the session, its name for the username, in the JSP using session.getAttribute ("username") to get username value, but in the EL, then using the $ (sessionScope.username) to obtain its value.

cookie

The so-called cookie is a small text file that is key, value approach to Session Tracking the contents of the documents recorded in this text, this text file is usually found in the browser's temporary storage area. JSTL and set the cookie does not provide the action, because this action is usually the back-end developers have to do things, not the front-end developer. If we set a name in cookie value for the userCountry, you can use $ (cookie.userCountry) to get it.

header and headerValues

header store the user's browser and server to communicate with the data, when the user requests a Web page server will send a written request for information in the header file, for example: the user's browser version, the user's computer and other areas set data. If want to get the user's browser version, that is, $ (header ["User-Agent"]). Another chance at a little bit under the same header name may have different values, this time must be used instead headerValues to get these values.

Note: Because the User-Agent contains "-," the special characters, it must not be written using the "[]",

$ (Header.User-Agent).

initParam

Like other properties, we can draw up a web site of the environmental parameters (Context), when we wish to obtain these parameters initParam as other properties, we can draw up a web site of the environmental parameters (Context), when we want to obtain 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) to get the name for the userid, the value of mike parameters. The following is the practice before: String userid = (String) application.getInitParameter ("userid");

param and paramValues
User parameters in obtaining what is commonly used methods:

request.getParameter (String name)
request.getParameterValues (String name)

You can use in the EL in both param and paramValues to obtain the data.
$ (Param.name)
$ (ParamValues.name)

Here param functions and request.getParameter (String name) the same, but paramValues and
request.getParameterValues (String name) the same. If the user fill a form, form name username, then we can use $ (param.username) to get the user fill in value.

pageContext

We can use $ (pageContext) to obtain user requirements or other relevant page for more information. The following table lists some commonly used parts

Expression
Explain

$ (PageContext.request.queryString)
Parameter string to obtain the requested

$ (PageContext.request.requestURL)
To obtain the requested URL, but does not include the parameters of the request string, the HTTP address of the servlet.

$ (PageContext.request.contextPath)
The name service webapplication

$ (PageContext.request.method)
HTTP access method (GET, POST)

$ (PageContext.request.protocol)
Made use of the protocol (HTTP/1.1, HTTP/1.0)

$ (PageContext.request.remoteUser)
Get username

$ (PageContext.request.remoteAddr)
To obtain the user's IP address

$ (PageContext.session.new)
Determine whether the new session, the so-called new session, said that while the client has just generated by the server not used

$ (PageContext.session.id)
To obtain the ID session

$ (PageContext.servletContext.serverInfo)
Host service to obtain information

This object can effectively improve the code hard-coding issues, such as page one A tag links to access a SERVLET, if coded the SERVLET the HTTP address then, if when the SERVLET the SERVLET-MAPPING time for a change need to modify the source code, This maintenance will be greatly reduced.

EL Arithmetic

Expression language supports arithmetic and logic operators are very much in the Java language in support of all the arithmetic operators, expression language can be used; even the Java language does not support some of the arithmetic operators and logical operators, the expression language also supports.

Code
<% @ Page contentType = "text / html; charset = gb2312"%>
<html>
<head>
<title> expression language - Arithmetic Operators </ title>
</ Head>
<body>
<h2> expression language - Arithmetic Operators </ h2>
<hr>
<table border="1" bgcolor="aaaadd">
<tr>
<td> <b> expression language </ b> </ td>
<td> <b> results </ b> </ td>
</ Tr>
<! - Direct output constant ->
<tr>
<td> \ $ (1) </ td>
<td> $ (1) </ td>
</ Tr>
<! - Calculation of the addition ->
<tr>
<td> \ $ (1.2 + 2.3) </ td>
<td> $ (1.2 + 2.3) </ td>
</ Tr>
<! - Calculation of the addition ->
<tr>
<td> \ $ (1.2E4 + 1.4) </ td>
<td> $ (1.2E4 + 1.4) </ td>
</ Tr>
<! - Calculation of subtraction ->
<tr>
<td> \ $ (-4 - 2) </ td>
<td> $ (-4 - 2) </ td>
</ Tr>
<! - Calculation of multiplication ->
<tr>
<td> \ $ (21 * 2) </ td>
<td> $ (21 * 2) </ td>
</ Tr>
<! - Computing division ->
<tr>
<td> \ $ (3 / 4) </ td>
<td> $ (3 / 4) </ td>
</ Tr>
<! - Computing division ->
<tr>
<td> \ $ (3 div 4) </ td>
<td> $ (3 div 4) </ td>
</ Tr>
<! - Computing division ->
<tr>
<td> \ $ (3 / 0) </ td>
<td> $ (3 / 0) </ td>
</ Tr>
<! - Calculated to request the ->
<tr>
<td> \ $ (10% 4) </ td>
<td> $ (10% 4) </ td>
</ Tr>
<! - Calculated to request the ->
<tr>
<td> \ $ (10 mod 4) </ td>
<td> $ (10 mod 4) </ td>
</ Tr>
<! - Calculation 3 project operator ->
<tr>
<td> \ $ ((1 == 2)? 3: 4) </ td>
<td> $ ((1 == 2)? 3: 4) </ td>
</ Tr>
</ Table>
</ Body>
</ Html>

Demonstrated the page above expression language supported by the add, subtract, multiply, divide, seeking more than other arithmetic function, the reader may also find the expression language also supports the div, mod and other operators. And the expression language as all values are floating point, so I 3 / 0 in essence, 3.0/0.0, get the results should be Infinity.

If you need the support of regular expression language of the page output, "$" symbol in the "$" symbol preceded by the escape character "\", otherwise the system that the "$" is the expression language specific tags.

EL relational operators

Shows examples of results of relational operators

== Or eq
Equal
$ (5 == 5) or $ (5eq5)
true

! = Or ne
Not equal
$ (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 language not only in the comparison between figures and figures, but also in the comparison between the characters and the characters, the string comparison is based on its corresponding UNICODE values to compare the size.

Note: When using EL relational operators can not be written:
$ (Param.password1) = = $ (param.password2)
Or
$ ($ (Param.password1) = = $ (param.password2))
Should be written as
$ (Param.password1 = = param.password2)

EL logical operator

Logical example of the results

& & Or and
Intersection $ (A & & B) or $ (A and B)
true / false

| | Or or
And set $ (A | | B) or $ (A or B)
true / false

! Or not
Non-$ (! A) or $ (not A)
true / false

Empty operator
$ (Empty A)
Empty operator A is mainly used to determine whether the null value or empty.

Conditional operator

$ (A? B: C)

分类:Java 时间:2010-07-18 人气:207
分享到:
blog comments powered by Disqus

相关文章

  • Jsp / Servlet review notes ----- JSP Expression Language Chapter 8 2010-10-26

    About 8.1 expression language JSP used to display dynamic content on the page, usually embedded in the JSP page, Java scripts to perform complex functions. But a lot of Java script JSP pages makes it difficult to maintain. A language similar to JavaS

  • Web Summary --- Servlet, JSP, filters and listeners, the scope and nine four built-in objects, EL expression language, custom tags, JSTL, an international collection of the Chinese character encoding 2011-01-01

    1, Servlet, ServletConfig: The former corresponds to a Servlet class, which corresponds to the configuration information in the web.xml (1) Servlet: init (ServletConfig): only then loaded and instantiated class is called once. service (ServletRequest

  • EL Expression Language 2010-03-25

    Basic syntax 1. EL Introduction 1. Grammatical structure ${expression} 2.[] And. Operator EL Provided. And [] Two operators to access data . When you want to access the property name contains special characters, such as . Or? And other symbols not le

  • JSTL Introduction: Expression Language (a) 2010-11-10

    JavaServer Pages (JSP) is a standard platform for J2EE presentation layer technology. JSP technology provides for the implementation of calculations (these calculations is used to dynamically generate page content) and scripting elements and actions.

  • OGNL expression language 2010-12-07

    Expression commonly used to access the page in a variety of variables, the results output. struts2 CPC supports the following expression language (the default is OGNL): OGNL: Object properties can easily operate the open source expression language; J

  • strtus2 the OGNL expression language and some labels 2010-04-23

    OGNL expression language: Relative EL expression, it provides some of the features we need in peacetime, such as: ? Support object method calls, such as xxx.sayHello (); ? Support class static method calls and the value of the visit, the expression o

  • JSP2.1 and unified expression language 2010-05-11

    JSP2.1 new features 1, the new unified expression language, an extension request to allow the value of the expression, the expression can be assigned either value, you can also call the method. 2, can be injected through the annotation of resources,

  • OGNL expression language introduced 2010-09-29

    OGNL expression language introduced OGNL Introduction OGNL Object-Graph Navigation Language is the abbreviation , It is a powerful expression language (Expression Language, Referred to as EL), Expression through its simple and consistent syntax, you

  • About OGNL expression language 2010-10-19

    OGNL is the abbreviation of the object graph navigation language, he is an open source project. Struts2 framework uses OGNL expression language as the default. Relative EL expression, it provides the usual features we need, such as: Support for objec

  • Struts 2 in the expression language: OGNL 2011-05-04

    Struts 2 in the expression language: OGNL Struts 2 supports the following expression language: 1.OGNL (Object-Graph Navigation Language), can easily operate the open source object property expression language; 2.JSTL (JSP Standard Tag Library), JSP 2

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.419 (s). 12 q(s)