I. El Overview
EL (Expression Language): The purpose is to make the JSP easier to write.
1.1, the role of El
After JSP2.0 the HTML and CSS, to separate the HTML and JS, the Java script to replace the label. Does not include <%...%>, etc.
and the benefits of tagging: It's easy to use even if you're not a Java person
1.2, the El format and use
Use format: ${}
Use premise: If you do not want to use EL, you need to specify iselignore= "true" in the JSP page directive, the default is false, that is, do not ignore
1.3. The EL operator
Operator |
Description |
Example |
Results |
+ |
Add |
${17+5} |
22 |
- |
Reducing |
${17-5} |
12 |
* |
By |
${17*5} |
85 |
/ or div |
Except |
${17/5} or ${17 Div 5} |
3 |
% or mod |
Take surplus |
${17%5} or ${17 mod 5} |
2 |
= = or eq |
Equals |
${5==5} or ${5 eq 5} |
True |
! = or ne |
Not equal to |
${5!=5} or ${5 ne 5} |
False |
< or lt |
Less than |
${3<5} or ${3 lt 5} |
True |
> or GT |
Greater than |
${3>5} or ${3 GT 5} |
False |
<= or le |
Less than or equal |
${3<=5} or ${3 le 5} |
True |
>= or ge |
Greater than or equal |
${3>=5} or ${3 ge 5} |
False |
&& or |
And |
${true&&false} or ${true and false} |
False |
! or not |
Non - |
${!true} or ${not true} |
False |
| | or or |
or |
${true| | False} or ${true or false} |
True |
Empty |
is empty |
${empty ""}, you can determine whether the string, data, collection length is 0, and 0 returns true. Empty can also be used with not or ! used together. ${not empty ""} |
True |
If the value of an EL expression is null, an empty string is displayed, which means nothing
Manipulate List and array :${list[0]},${arr[0]};
Manipulate Bean 's properties:${person.name},${person[' name '}, corresponding Person.getname () Method
Operation Map value:${map.key},${map[' key '}, corresponding to map.get (key)
1.4, El's built-in objects
EL has a total of one built-in object that can be used without creating it. Each of these built-in objects has a Map type, and the last one is The PageContext object.
Map Applicationscope (key) |
A collection of application-scoped scoped variables |
MAP Cookie |
A collection of all cookies |
Map Header |
HTTP request Header, string |
Map headervalues |
HTTP request header, string collection |
Map Initparam |
A collection of all application parameter names |
PageContext |
The Javax.servlet.jsp.PageContext object for the current page |
Map Pagescope (key) |
A collection of all objects within a page range |
Map param |
A collection of all request parameter strings |
Map paramvalues |
All request parameters as a collection of strings |
Map Requestscope (key) |
A collection of all request-scoped objects |
Map Sessionscope (key) |
A collection of all session-scoped objects |
El domain object related:
L Pagescope:${pagescope.name} equals with Pagecontext.getattribute ("name"); l Requestscope:${requestscope.name} Equivalent to Request.getattribute ("name"); L Sessionscoep: ${sessionscope.name} equals with Session.getattribute ("name"); l Applicationscope:${applicationscope.name} equals to Application.getattribute ("name");
Use El to get JavaBean objects persisted in the domain, then JavaBean must set the Get method
Global lookup:${person} indicates that in turn, the pagescope,requesscopet, Sessionscope,appliationscope Four fields looking for a property named person
Attention:
El expression when obtaining the value of map or JavaBean, there are two ways to request.xxx or request[' xxx '], the same effect, but if the map key or JavaBean property name contains ' _ ', El must use subscript way request[' xx_x ']
PageContext Object
PageContext:pagecontext is the pagecontext type! You can use the pagecontext object to call the GetXXX () method, such as pagecontext.getrequest () , you can ${pagecontext.request} . that is, read the JavaBean Property!
EL Expression |
Description |
${pagecontext.request.querystring} |
Pagecontext.getrequest (). getquerystring (); |
${pagecontext.request.requesturl} |
Pagecontext.getrequest (). Getrequesturl (); |
${pagecontext.request.contextpath} |
Pagecontext.getrequest (). Getcontextpath (); |
${pagecontext.request.method} |
Pagecontext.getrequest (). GetMethod (); |
${pagecontext.request.protocol} |
Pagecontext.getrequest (). Getprotocol (); |
${pagecontext.request.remoteuser} |
Pagecontext.getrequest (). Getremoteuser (); |
${PAGECONTEXT.REQUEST.REMOTEADDR} |
Pagecontext.getrequest (). GETREMOTEADDR (); |
${pagecontext.session.new} |
Pagecontext.getsession (). IsNew (); |
${pagecontext.session.id} |
Pagecontext.getsession (). GetId (); |
${pagecontext.servletcontext.serverinfo} |
Pagecontext.getservletcontext (). Getserverinfo (); |
Second, El function library
1. What is the El function library
The El function library is an extension of the El by a third party ,
the El function library defines some static methods that have return values. They are then called through the El language!
The EL Library contains many ways to manipulate strings, as well as operations on collection objects. For example:${fn:length ("abc")}, but not many
Because it is a third-party thing, it needs to be imported. Import requires the use of the taglib directive!
<%@ taglib prefix= "FN" uri= "Http://java.sun.com/jsp/jstl/functions"%>
Common methods:
l String toUpperCase (String input): L string toLowerCase (String input): L int indexOf (string input, String substring): L Boolean cont Ains (string input, String substring): L boolean containsignorecase (string input, String substring): L Boolean StartsWith ( string input, String substring): L boolean endsWith (string input, String substring): l string substring (string input, int be Ginindex, int endIndex): l string Substringafter (string input, String substring): Hello-world, "-" L Substringbefore ( string input, String substring): Hello-world, "-" L string escapexml (String input): Put the string ">", "<" ... Escaped! L string Trim (String input): l string Replace (string input, String substringbefore, String substringafter): L string[] Split (string input, string delimiters): L int Length (Object obj): You can get the lengths of strings, arrays, various collections! L string Join (String array[], string separator):
<% @taglib prefix= "FN" uri= "http://java.sun.com/jsp/jstl/functions"%> String[] STRs = {"A", "B", "C"}; List List = new ArrayList (), List.add ("a");p Agecontext.setattribute ("arr", STRs);p agecontext.setattribute ("list", list);%>${fn:length (arr)}<br/><!--3-->${fn:length (list)}<br/><!--1-->${FN: toLowerCase ("Hello")}<br/> <!--hello-->${fn:touppercase ("hello")}<br/> <!--Hello-->${fn: Contains ("ABC", "A")}<br/><!--true-->${fn:containsignorecase ("abc", "Ab")}<br/><!--true-- >${fn:contains (arr, "a")}<br/><!--true-->${fn:containsignorecase (list, "a")}<br/><!-- True-->${fn:endswith ("Hello.java", ". Java")}<br/><!--True-->${fn:startswith ("Hello.java", "Hell") }<br/><!--True-->${fn:indexof ("Hello-world", "-")}<br/><!--5-->${fn:join (arr, ";")} <br/><!--a;b;c-->${fn:replace ("Hello-world", "-", "+")}<br/><!--Hello+world-->${Fn:join (Fn:split ("A;B;C;", ";"), "-")}<br/><!--a-b-c-->${fn:substring ("0123456789", 6, 9)}<br/> <!--678-->${fn:substring ("0123456789", 5,-1)}<br/><!--56789-->${fn:substringafter (" Hello-world ","-")}<br/><!--World-->${fn:substringbefore (" Hello-world ","-")}<br/><!--Hello -->${fn:trim ("a b C")}<br/><!--a b C-->${fn:escapexml ("
Similarly, you can alsoCustom El functions
1. Write a class that writes a static method with a return value
2. Write Tld file, can refer to Jstl given fn.tld file to write, the tld file to /web-inf directory under
Package Cn.cil.funcations;public class Myfuncations {public static String get () {return ' Custom El function library test ';}}
Tld<?xml version= "1.0" encoding= "UTF-8"? ><taglib 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/http Java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd " version=" 2.0 "> <tlib-version>1.0</ tlib-version> <short-name>cc</short-name> <uri>http://www.xixihahaxiaobenzhu.cn/ jsp/functions</uri> <function> <name>get</name> <function-class> Cn.cil.funcations.myfunction</function-class> <function-signature>string get () </ Function-signature> </function></taglib>
Jsp<body>
Considerations for customizing the El function:Once you have finished writing the TLD, you need to place it in any subdirectory in the <web application >\web-inf directory or in the Web-inf directory except for the classes and LIB directories.
The <uri> element in the TLD file uses the URI of the specified TLD file, which in the JSP file needs to be introduced into the tag library description file.
The <function> element is used to describe an El custom function, where:
The <name> child element is used to specify the name of the El Custom function.
The <function-class> child element is used to specify the full Java class name,
The <function-signature> child element is used to specify the signature of a static method in a Java class, and the method signature must indicate the type of the method's return value and the type of each parameter, with commas between the parameters
Javaweb's El Chapter