Java EE (7) _el expression language

Source: Internet
Author: User
Tags tld

Introduction of El ExpressionEL Full name is the main function of expression Language.el:Get Data:El Expressions are primarily used to replace script expressions in JSP pages to retrieve Java objects from various types of Web domains and get data. (objects in a web domain, accessing JavaBean properties, accessing the list collection, accessing the map collection, accessing an array)perform the operation:• Use El expressions to perform some basic relational, logical, and arithmetic operations on JSP pages to do some simple logic operations on JSP pages. ${user==null}get web Development common objectsEl Expressions define implicit objects that make it easy for web developers to get a reference to a common Web object to get the data in those objects.calling the Java methodThe El Expression allows the user to develop a custom El function that invokes a method of the Java class through an El expression in a JSP page.II. acquisition of data1. Get data syntax using el expression: "${identifier}"2, the EL Expression statement when executing, will call the Pagecontext.findattribute method, with the identifier as the keyword, respectively, from page, request, session, application four fields to find the corresponding object, Found returns the corresponding object, and returns "" (Note that it is not NULL, but an empty string, which is excellent).3. Example code:
<br/>----------------get data stored in the domain-------------------------<br/> <%String Data= "ABCD"; Request.setattribute ("Data", data);%>${data}<%--pagecontext.findattribute ("Data") ""--%><br/>----------------get data for JavaBean stored in the domain---------------- <br/><%Person P=NewPerson ();p. SetName ("AAAA"); Request.setattribute ("Person", p);%>${person.name}<!--is obtained from the Get method instead of the property in the person class--><br/>----------------gets the data from JavaBean in the JavaBean stored in the domain-----<br/> <%Person P1=NewPerson (); Address a=NewAddress (); A.setcity (Shanghai);p 1.setAddress (a); Request.setattribute ("P1", p1);%>${p1.address.city}<br/>----------------get data from the list collection stored in the domain-----------------<br/> <%List List=NewArrayList (); List.add (NewPerson ("AAA")); List.add (NewPerson ("BBB")); Request.setattribute ("List", list);%>${list[' 1 '].name} <!--. to get the data in the Map collection in the domain using the []--><br/>------------------------------------<br/> <%map Map=NewHashMap (); Map.put ("AA",NewPerson ("AAAAA")); Map.put ("BB",NewPerson ("BBBBB")); Map.put ("CC",NewPerson ("CCCCC")); Map.put ("DD",NewPerson ("ddddd")); Map.put ("111",NewPerson ("eeeee")); Request.setattribute ("Map111", map);%>${map111.cc.name}<br/>----------------A commonly used El expression-------------------------<br/>${pagecontext.request.contextpath}<a href= "${pagecontext.request.contextpath}/index.jsp" > Points </a>
Third, perform the operation

1, Syntax: ${expression},el expression supports the following operators (as well as operators such as subtraction, but does not support string concatenation, like a bug)

2, empty operator: Check whether the object is null or "empty", very useful!!! 3, two Yuan expression: ${user!=null?user.name: ""}, very useful!!! 4, [] and. operator,. Use []5. Example:
<br/>----------------operator-------------------------<br/><%Request.setattribute ("username", "AAA"); Request.setattribute ("Password", "124");%>${username= = ' aaa ' && password== ' 123 ' }<br/>----------------The empty operator---------------------<br/><% 1, request.setattribute ("list",NULL); 2, Request.setattribute ("list",NewArrayList ());%>${empty (list)}<br/>----------------Two-dollar operator-------------------------<br/>${user!=NULL? User.username: " }<br/>----------------Two-dollar operator (data echo)-------------------------<br/><%Request.setattribute ("Gender", "male");%><input type= "Radio" name= "Gender" value= "male" ${gender== ' male '? ' Checked ': '}>male<input type= "Radio" name= "Gender" vlaue= "female" ${gender== ' female '? ' Checked ': '}> female
Iv. getting the common objects of web development1. The El expression language defines 11 hidden objects, which make it easy to get some common objects in web development and read the data of those objects.2. Syntax: ${implicit object name}: Getting a reference to an object3. Example:
${pagecontext} <!--why not go to the domain to take the people to pageconetxt value? --><br/>----------------access the specified domain-------------------------using an El implicit object <br/><%Pagecontext.setattribute ("AA", "123");%>${aa}<!--This is a lookup from four domains${pagescope.aa}<!--find from the Pointing field${sessionscope.user!=NULL}<br/>----------------Gets the map object that holds all the request parameters----------------------<br/>${param.name}${paramvalues.name[0]}${paramvalues.name[1]}<br/>----------------GET request Header-------------------------<br/>${header[' Accept-language '} <!--with []. No, there's a special symbol--><br/>---------------get cookies-------------------------< Br/>${cookie. Jsessionid.name}<br/>${cookie. Jsessionid.value}${initparam.xx}
V. Invoking the Java method with El1. The EL expression syntax allows developers to develop custom functions to invoke methods of the Java class.• Example: ${prefix:method (params)}• Only static methods that are called in the El Expression can be Java classes.• This Java class static method needs to be described in the TLD file before it can be called by an El expression.The El Custom function is used to extend the functionality of the El expression, allowing the El expression to do what ordinary Java program code can do.• This feature allows the EL expression to remove Java code, but cannot remove the web-related code because the El does not have a related object to the Web development, which is the meaning of the label's existence.2. EL function Development Stepsin general, the development and application of El Custom functions consists of the following three steps:• Write a static method of a Java class• Write a tag library descriptor (TLD) file that describes the custom function in the TLD file.• Import and use custom functions in JSP pages* Generally do not develop the El function, El has its own definition of the function library (are some of the functions of the operation string)3, because in the JSP page display data, often need to deal with the displayed string, Sun company needle for some common processing defines a set of El function library for developers to use. These El functions are described in the Jstl development package, so use the Sun Company's El Library in the JSP page , you need to import the JSTL development package and import the El function library into the page as follows:use the EL function defined by Jstl in the page:
<% @taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "FN"%>

Instance:

${fn:tolowercase ("AAAA") }<%Request.setattribute ("Arr",NewString[5]);%>${fn:length (arr)}<%List List=NewArrayList (); List.add ("AA"); List.add ("BB"); Request.setattribute ("List", list);%><c:foreach var= "i" begin= "0" end= "${fn:length (list)}" >${list[i]}</c:forEach>${fn:join (Fn:split ("WWW,ITCAST,CN", ","), ".")}${fn:contains ("AAAABBBCC", "AB")}${fn:escapexml ("<a href= > Dot </a>")}<br/>---------------Take out user-filled preferences to echo------------------------<br/> <%Request.setattribute ("Likes",Newstring[]{"Football", "Sing"});%><input type= "checkbox" Name= "likes" value= "Sing" ${fn:contains (Fn:join (Likes, ","), "Sing")? ' Checked ': '}>singing<input type= "checkbox" Name= "likes" value= "Dance" ${fn:contains (Fn:join (Likes, ","), "Dance")? ' Checked ': '}>Dancing<input type= "checkbox" Name= "likes" value= "basketball" ${fn:contains (Fn:join (Likes, ","), "basketball")? ' Checked ': '}>Blue Ball<input type= "checkbox" Name= "likes" value= "Football" ${fn:contains (Fn:join (Likes, ","), "football")? ' Checked ': '}> football

Java EE (7) _el expression language

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.