Use El expressions to intercept string/get list length instances. el expressions

Source: Internet
Author: User
Tags null null

Use El expressions to intercept string/get list length instances. el expressions

$ {Fn: substring (wjcd. lrsj, 0, 16 )}

Use the functions function to obtain the list length.

${fn:length(list)}<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 

Function Description Example

Fn: containsDetermine whether the string contains another string <c: if test = "$ {fn: contains (name, searchString)}">

Fn: containsIgnoreCaseDetermine whether the string contains another string (case-insensitive) <c: if test = "$ {fn: containsIgnoreCase (name, searchString)}">

Fn: endsWithJudge whether the string ends with another string <c: if test = "$ {fn: endsWith (filename,". txt ")}">

Fn: escapeXmlConvert some characters to XML Representation. For example, the <character should be converted to <$ {fn: escapeXml (param: info )}

Fn: indexOfPosition of the substring in the parent string $ {fn: indexOf (name ,"-")}

Fn: joinCombine the data in the array into a new string and open $ {fn: join (array, ";")} with the specified character lattice ,";")}

Fn: lengthGet the length of the string or the size of the array $ {fn: length (shoppingCart. products )}

Fn: replaceReplace the specified character $ {fn: replace (text, "-", "•")} In the string ,"-","•")}

Fn: splitSplit the string by the specified character $ {fn: split (customerNames ,";")}

Fn: startsWithDetermine whether the string starts with a substring <c: if test = "$ {fn: startsWith (product. id," 100-")}">

Fn: substringGet substring $ {fn: substring (zip, 6,-1 )}

Fn: substringAfterObtain the substring starting from the position of a character $ {fn: substringAfter (zip ,"-")}

Fn: substringBeforeObtain the substring from the start to the position of a character $ {fn: substringBefore (zip ,"-")}

Fn: toLowerCaseConvert to lower case $ {fn. toLowerCase (product. name )}

Fn: toUpperCaseConvert to UpperCase characters $ {fn. UpperCase (product. name )}

Fn: trimRemove spaces before and after the string $ {fn. trim (name )}

Function Description

• F n: contains (string, substring)Returns true if the string parameter contains the substring parameter.

• Fn: containsIgnoreCase (string, substring)Returns true if the string parameter contains the substring (Case Insensitive ).

• Fn: endsWith (string, suffix)Returns true if string ends with suffix.

• Fn: escapeXml (string)Convert the XML (HTML) with special meaning to the corresponding XML character entity code, and return

• Fn: indexOf (string, substring)Returns the position where the substring parameter appears for the first time in the string parameter.

• Fn: join (array, separator)Concatenates a given array with a given delimiter separator to form a new string and return it.

• Fn: length (item)Returns the number of elements contained in the parameter item. The parameter Item type is array, collection, or String. For the String type, the return value is the number of characters in the String.

• Fn: replace (string, before, after)Returns a String object. Replace all places where the before parameter appears in the parameter string with the parameter after string, and return the replaced result.

• Fn: split (string, separator)Returns an array that uses the separator parameter as the delimiter to split the string parameter. Each part of the split is an element of the array.

• Fn: startsWith (string, prefix)Returns true if the string parameter starts with the prefix.

• Fn: substring (string, begin, end)Returns the string part of the parameter, starting from the begin parameter to the end position of the parameter, including the character of the end position.

• Fn: substringAfter (string, substring)Returns the part of the substring that follows the string parameter.

• Fn: substringBefore (string, substring)Returns the part of the substring before the string parameter.

• Fn: toLowerCase (string)Convert all characters of the string parameter to lowercase and return

• Fn: toUpperCase (string)Converts all characters of the string parameter to uppercase and returns

• Fn: trim (string)Remove the spaces at the beginning and end of the string parameter and return it.
 

I. Introduction to JSP EL

1. syntax structure $ {expression}

2. The [] and. Operators EL provide "." and "[]" operators to access data.

When the attribute name to be accessed contains some special characters, such as. Or? If it is not a letter or number, you must use "[]". Example: $ {user. my-Name} should be changed to $ {user ["My-Name"]}. If you want to dynamically set the value, you can use "[]" instead of ". "dynamic values cannot be achieved. For example, in $ {sessionScope. user [data]}, data is a variable. 3. The method for variable EL to access variable data is simple, for example, $ {username }. It means to retrieve the variable named username in a range. Because we do not specify the username of the range, it will search for the range from Page, Request, Session, and Application in sequence. If the username is found on the way, it will be directly returned and will not be found any more, but if all the ranges are not found, null will be returned. Name of the attribute range in EL Page pagination Request RequestScope Session SessionScope Application ApplicationScope

Ii. Valid expressions in JSP EL

A valid expression can contain text, operators, variables (Object references), and function calls. We will understand each of these valid expressions:

1. Text

The JSP Expression Language defines the following words that can be used in an expression:

 
Text Value

Boolean

True and false
 
Integer

Similar to Java. Can contain any positive or negative number, such as 24,-45, 567
 
Floating Point

Similar to Java. Can contain any positive or negative floating point numbers, such as-1.8E-45, 4.567
 
String

Any string limited by single or double quotation marks. For single quotes, double quotes, and backslash, The backslash character is used as the escape sequence. Note that if double quotation marks are used at both ends of a string, the single quotation marks do not need to be escaped.
 
Null null


2. Operators

JSP expression language provides the following operators, most of which are commonly used operators in Java:

 
Definition

Arithmetic type

+,-(Binary), *,/, div, %, mod,-(one dollar)
 
Logical type

And, &, or, | ,! , Not
 
Relational

=, Eq ,! =, Ne, gt, <=, le,> =, ge. It can be compared with other values or with Boolean, String, integer, or floating-point text.
 
Null

The null operator is a prefix operation that can be used to determine whether the value is null.
 
Condition? B: C. Assign values to B or C based on the value of.


3. Implicit object

JSP Expression Language defines a set of implicit objects, many of which are available in JSP scriplet and expressions:

Definition

PageContext

The context of the JSP page. It can be used to access JSP implicit objects, such as requests, responses, sessions, outputs, and servletContext. For example, $ {pageContext. response} assigns a value to the response object of the page.
 


In addition, it provides several implicit objects that allow simple access to the following objects:

 
Definition

Param

Map the request parameter name to a single String parameter value (obtained by calling ServletRequest. getParameter (String name ). The getParameter (String) method returns a parameter with a specific name. Expression $ (param. name) is equivalent to request. getParameter (name ).
 
ParamValues

Map the request parameter name to a numeric array (obtained by calling ServletRequest. getParameter (String name ). It is very similar to a param implicit object, but it retrieves a string array instead of a single value. Expression $ {paramvalues. name) is equivalent to request. getParamterValues (name ).
 
Header

Map the request header name to a single String header value (obtained by calling ServletRequest. getHeader (String name ). The expression $ {header. name} is equivalent to request. getHeader (name ).
 
HeaderValues

Map the request header name to a numeric array (obtained by calling ServletRequest. getHeaders (String ). It is very similar to the header implicit object. The expression $ {headerValues. name} is equivalent to request. getHeaderValues (name ).
 
Cookie maps the cookie name to a single cookie object. Client requests sent to the server can obtain one or more cookies. Expression $ {cookie. name. value} returns the first cookie value with a specific name. If the request contains multiple cookies with the same name, use the $ {headerValues. name} expression.

InitParam maps the context initialization parameter names to a single value (obtained by calling ServletContext. getInitparameter (String name ).


In addition to the preceding two types of implicit objects, some objects allow access to variables in various scopes, such as Web context, session, request, and page:

 
Definition

Pagination

Map the variable names in the page range to their values. For example, an EL expression can use $ {pagination. objectName} to access a page range object in a JSP, and use $ {pagination. objectName. attributeName} to access the object attributes.
 
RequestScope

Map the variable name in the request range to its value. This object allows access to the properties of the request object. For example, the EL expression can use $ {requestScope. objectName} to access an object in the JSP request range, and use $ {requestScope. objectName. attributeName} to access the attributes of the object.
 
SessionScope

Map the name of the variable in the session range to its value. This object allows access to the properties of the session object. For example:

$ SessionScope. name}
 
ApplicationScope

Map the variable names in the application range to their values. This implicit object allows access to objects within the application range.
 


Iii. Special emphasis:

1. Note that when the expression references these objects based on their names, the corresponding objects instead of the corresponding attributes are returned. For example, even if the existing pageContext attribute contains some other values, $ {pageContext} returns the PageContext object.

2. Note that <% @ page isELIgnored = "true" %> indicates whether EL language is disabled. TRUE indicates that EL language is disabled. FALSE indicates that EL language is disabled by default in JSP2.0.

The above example of using El expressions to intercept the string/obtain the length of list is all the content shared by Alibaba Cloud. I hope you can provide a reference and support for the customer's house.

Related Article

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.