EL expression,

Source: Internet
Author: User
Tags tld

EL expression,

1. EL is the built-in Expression Language of JSP.

  • Starting with jsp2.0, instead of using Java scripts, we use EL expressions and dynamic tags to replace Java scripts.
  • EL replaces <% =... %>, that is, EL can only be used for output.

2. EL expression to read four fields

  • $ {Xxx}: the global search for the property named xxx. If it does not exist, an empty string is output instead of null.
  • $ {Pagination. xxx}, $ {requestScope. xxx}, $ {sessionScope. xxx}, and $ {applicationScope. xxx} specify the domain to obtain attributes.

3. Navigation of JavaBean

1 <% @ page contentType = "text/html; charset = UTF-8" language = "java" %> 2 <% @ page import = "domain. employee, domain. address "%> 3 
1 package domain; 2 public class Employee {3 private String name; 4 private double salary; 5 private Address address Address; 6 7 public String getName () {return name ;} 8 public void setName (String name) {this. name = name;} 9 public double getSalary () {return salary;} 10 public void setSalary (double salary) {this. salary = salary;} 11 public Address getAddress () {return address;} 12 public void setAddress (Address address Address) {this. address = address;} 13 @ Override14 public String toString () {15 return "Employee {" + 16 "name = '" + name +' \ '+ 17 ", salary = "+ salary + 18", address = "+ address + 19'} '; 20} 21 public String getHehe () {return" Haha ";} 22}
 1 package domain; 2  3 public class Address { 4     private String city; 5     private String street; 6  7     public String getCity() { return city; } 8     public void setCity(String city) { this.city = city; } 9     public String getStreet() { return street; }10     public void setStreet(String street) { this.street = street; }11     @Override12     public String toString() {13         return "Address{" +14                 "city='" + city + '\'' +15                 ", street='" + street + '\'' +16                 '}';17     }18 }

4. Among the 11 built-in objects that can be output by EL, 10 of them are Map, and the PageContext type is not map, and the top 9 objects are respectively.

  • Pagination
  • RequestScope
  • SessionScope
  • ApplicationScope
  • Param: corresponds to a parameter. It is a Map, with the key parameter name and value being the parameter value. This parameter is applicable to single-value parameters;
  • ParamValues: corresponds to a parameter. It is a Map. The key parameter name and value are multiple parameter values, which are suitable for multiple worthy parameters. Parameters are obtained from the address bar;
  • <Body>
    <% -- Map. key this is el syntax. map ['key'] can also operate map -- %>
    $ {Param. username} <br/>
    $ {ParamValues. holobby [0]}
    $ {ParamValues. holobby [1]}
    </Body>
  • Header: corresponds to the request header. It is a Map, where key indicates the header name and value indicates a single header value, which is applicable to single-value request headers.

<Body >$ {header ['user-agent']} <br/> </body>

  • HaeaderValues: corresponds to the Request Header, which is a Map, where key represents the header name and value is a multi-head value, which is applicable to multi-value request headers.
  • InitParam: Get the parameters in <context-param>
  • <context-param>      <param-name>xxx</param-name>       <param-value>XXX</param-value></context-param>    <context-param>      <param-name>yyy</param-name>      <param-value>YYY</param-value></context-param>
    <body>
    ${initParam.xxx}
    </body>
  • Cookie: Map <String, Cookie> type, where key is the cookie name, value is the cookie object, $ {cookie. username. value}
  • <body>${cookie.JSESSIONID}<br/>${cookie.JSESSIONID.name}<br/>${cookie.JSESSIONID.value}<br/></body>

  • PageContext: it is of the PageContext type.
1 <body> 2 <! -- Get the project name. If none, it is "/" --> 3 $ {pageContext. request. contextPath} 4 

  
EL expression Description Description
$ {PageContext. request. queryString} PageContext. getRequest (). getQueryString (); GET request parameter string
$ {PageContext. request. requestURL} PageContext. getRequest (). getRequestURL (); Obtains the request URL, but does not include the request parameter string.
$ {PageContext. request. contextPath} PageContext. getRequest (). getContextPath (); Name of the web application of the service
$ {PageContext. request. method} PageContext. getRequest (). getMethod (); Get http (GET, POST)
$ {PageContext. request. protocol} PageContext. getRequest (). getProtocol (); Obtain the used protocol (HTTP/1.1, HTTP/1.0)
$ {PageContext. request. remoteUser} PageContext. getRequest (). getRemoteUser (); Get User Name
$ {PageContext. request. remoteAddr} PageContext. getRequest (). getRemoteAddr (); Obtain the user's IP address
$ {PageContext. session. new} PageContext. getSession (). isNew (); Determine whether the session is new
$ {PageContext. session. id} PageContext. getSession (). getId (); Obtains the session ID.
$ {PageContext. servletContext. serverInfo} PageContext. getServletContext (). getServerInfo (); Obtain host service information

5. EL function library (provided by JSTL)

  • Import the tag Library:
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
${fn:contains("abc","a")}<!-- true -->
Function Description
Boolean contains (String input, String substring) Test whether the input string contains the specified substring.
Boolean containsIgnoreCase (String input, String substring) Test whether the input string contains the specified substring. It is case insensitive.
Int indexOf (String input, String substring) Returns the position where the specified string appears in the input string.
String toLowerCase (String input) Converts characters in a string to lowercase letters.
String toUpperCase (String input) Converts the characters in the string to uppercase.
Boolean startsWith (String input, String substring) Test whether the input string starts with the specified prefix.
Boolean endsWith (String input, String substring) Test whether the input string ends with the specified suffix.
String substring (String input, int beginIndex, int endIndex) Returns the subset of a string.
String substringAfter (String input, String substring) Returns the subset of a string after the specified substring.
String substringBefore (String input, String substring) Returns the subset of a string before the specified substring.
String escapeXml (String input) Skip the characters ("<", ">", "&", "'", ") that can be marked as XML characters, that is, escape
String trim (String input) Remove trailing blank characters
String replace (String input, String substringBefore, String substringAfter) Replace the specified position in the input string with the specified string and then return
String [] split (String input, String delimiters) Separate the string with the specified separator and form a substring array and return
Int length (Object obj) Returns the length of a string, array, and collection.
String join (String array [], String separator) Combine the elements in the array into a string and then output

6. Custom function library

① Write a java class, which can be defined as 0 ~ N methods, but must be static and return values.

② Create a tld file under the WEB-INF directory

<function>      <name>fun</name>      <function-class>fn.MyFunction</function-class>      <function-signature>java.lang.String fun()</function-signature></function>

③ Import the tag Library to the jsp page

<%@ taglib prefix="it" uri="/WEB-INF/tlds/itcast.tld"%>

④ Use a custom function in the jsp page: $ {it: fun ()}

 

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.