JSP programming topic 2 JSP core Two (El expression, El Operator and El Built-in objects in addition to four domain property spaces)

Source: Internet
Author: User

Tag: El expression gets data from a specified field the properties of the Ask Bean gets the array and the map and the elements in the list El operator in addition to the El built-in objects associated with four domain property spaces

1. El Expression:

-El,expression Language, Expression language, is an easy way to get data in a JSP page. The EL expression is a concept introduced from the JSP2.0 version.

-The basic syntax of the EL expression is simple: any static part of the JSP page can get the value of the specified expression in the form of ${expression}.

-It is important to note that El can only fetch data from four major domains. The order in which they are found is: The property value of the specified name is looked up from the four fields in the range from small to large. (Pagecontext,request,session,applicaiton)

Such as:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/22/2bdadddc38c0f04b517fd896f06564dd.png "title=" 07.png "alt=" 2bdadddc38c0f04b517fd896f06564dd.png "/>


A. Get data from the specified domain:

-Finding the application domain space from the PageContext will reduce the efficiency of execution. If a property is determined to reside in a domain property space, you can specify to find it directly from that space. In this case, you need to take advantage of El's four built-in objects related to attribute space:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/22/8a2d5715229bade5ca2265ab92fbf1e7.png "title=" 06.png "alt=" 8a2d5715229bade5ca2265ab92fbf1e7.png "/>


b, accessing the Bean's properties: (You can use the dot operator multiple times)

-El can get to the specified object property value by ${key. Property}. The actual call to the underlying is the Get method for the corresponding property of the object.

-Of course, it can also be obtained using ${key[' property '} or ${key["property"}, which is not commonly used.

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/22/08bb46877fa38f31faa5db871eb33ef5.png "title=" 08.png "alt=" 08bb46877fa38f31faa5db871eb33ef5.png "/>


c, get the elements in the array:

-El can get the element to the specified index by ${key [index]}. However, it is important to note that the system does not throw an array out-of-bounds exception if the element in the array does not exist for that specified index.

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/22/0601404f3d5191b92812009da673979f.png "title=" 09.png "alt=" 0601404f3d5191b92812009da673979f.png "/>


D. Get the elements in the list:

-the element of the specified index in the list can be obtained in the same way as the element in the get array, by ${key[Index]}. If no element of the specified index exists in the list, the system does not throw an out-of-bounds exception. (Cannot use El expression to access set because set is unordered, no index concept)

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/22/3b7f5bff44c6571a57c4bff2b500aded.png "title=" 10.png "alt=" 3b7f5bff44c6571a57c4bff2b500aded.png "/>


E, get the element in map: El through ${attributename.mapkey} can get the value of the specified key of the specified map:

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/22/8236202967279a1242fd4e55d4b6f44e.png "title=" 11.png "alt=" 8236202967279a1242fd4e55d4b6f44e.png "/>


2. El Operator:

-El expressions can perform various operations, where the commonly used operators are:

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/22/05dea0d3757abbf849700caf01257614.png "title=" 12.png "alt=" 05dea0d3757abbf849700caf01257614.png "/>

-In addition to the above operators, there is a very useful operator, empty, whose usage is the ${empty variable}, and the result is a Boolean value. If the variable is defined, the return value is true, if the variable is of type string and its value is a null string, the return value is true, if the variable is a reference type, and its value is NULL, the return value is "true" if the variable is of the collection type and it does not contain any elements.

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/22/2ce943a169e642f0408c829ecf1872da.png "title=" 13.png "alt=" 2ce943a169e642f0408c829ecf1872da.png "/>


3. In addition to the El built-in objects associated with four domain attribute spaces:

-Just like the Java code block of JSP and the expression block can use nine built-in objects, in the El expression, there is also a built-in object, and there are 11 built-in objects. Common built-in objects, in addition to the built-in objects related to the four domain property spaces previously used, are as follows:


A, PageContext:

-The PageContext is the same object as the PageContext in the JSP built-in object. Through this object, you can get to the request, response, session, ServletContext, ServletConfig and other objects. Note that these objects are not built-in objects in El. These objects can only be obtained through PageContext.

-Use ${pagecontext.request} directly in El to get the request object. Of course, the Pagecontext.getrequest () method is actually called at the bottom. Similarly, you can get to other objects in a similar way.

-Among these acquired objects, one is the most commonly used in the actual project: ${pagecontext.request.contextpath}, which is used to get the name of the current project published to the server, typically used in front of the path of the JSP page.

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/22/9800b27a6882f8e7c97f13d51adbf3df.png "title=" 14.png "alt=" 9800b27a6882f8e7c97f13d51adbf3df.png "/>

-In El's 11 built-in objects, the other 10 built-in objects, except PageContext, are of type Java.util.Map.


b, param:

The specified parameter value in the request is obtained by ${param. Parameter name} in El, and the underlying actual invocation is: Request.getparameter (), for example, the submitted request is:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/22/6d14f278dfff8181be73511400aad7cb.png "style=" float: none; "title=" 15.png "alt=" 6d14f278dfff8181be73511400aad7cb.png "/>

-In the JSP page, you can get the value of the name parameter to ABC in the following way:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/22/82ef45129690208dcdda16cf3baf48b7.png "style=" float: none; "title=" 16.png "alt=" 82ef45129690208dcdda16cf3baf48b7.png "/>


C, Paramvalues:

-If the requested request has more than one value in the same parameter, it can be obtained by ${paramvalues. Parameter name [index]} to the parameter value of the specified index number. For example, the submitted request is:

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/22/d64d9c5bcd29b22fc19426e952f2fc05.png "title=" 18.png "alt=" D64d9c5bcd29b22fc19426e952f2fc05.png "/>

-The specified parameter values in the request can be obtained in the JSP page as follows:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/22/bc1425c079a8d10222674ba1d72f890a.png "title=" 19.png "alt=" Bc1425c079a8d10222674ba1d72f890a.png "/>


D. If you want to get the contents of sub-tags <param-value> in the <context-param> tag in Web. XML, you can pass the contents of the ${initparam.<param-name> tab} To obtain:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/22/e5008b607837964ad73aedd4ef77832b.png "title=" 20.png "alt=" E5008b607837964ad73aedd4ef77832b.png "/>

-In the JSP page, get it in the following way:

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/22/f991595a936a6589359f5a28618a872a.png "title=" 21.png "alt=" F991595a936a6589359f5a28618a872a.png "/>





This article is from the "12392717" blog, please be sure to keep this source http://12402717.blog.51cto.com/12392717/1975027

JSP programming topic 2 JSP core Two (El expression, El Operator and El Built-in objects in addition to four domain property spaces)

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.