Java Web-EL expression in JSP

Java Web-EL expression in JSP

concept

EL (Expression Language) is a language expression can be simplified and the replacement writing JSP pages JAVA code

grammar

{$ <Written here expression >}

Generally only used to write expressions, do not write too complicated things, responsible for business-related JavaBean

Result of the expression as a resource for direct display HTML

effect

  1. Operation

    Supported operators:

    1. Arithmetic operators: + - * / (may also be represented by div)% (may also be represented by mod)

    2. Comparison operators> <> = <= ==! =

    3. The logical operators; && ||!

    4. Air Operator: empty: determining a string, set, or whether the object is null array length is zero

      For example: $ {empty list}, To return true, list or length must be Null 0

      Can be inverted: $ or $ {not empty list} {empty list!}

      For strings, List, map types are available, etc.

  2. Get the value

    Note, EL expression can only get the value from the domain objects

    Syntax: There are two grammar

    1. $ {Domain name key name.}: Gets the value of the specified key from the specified domain, if there is, then it returns an empty string

      Domain Name:

      1. pageScope: get the value from the domain pageContext
      2. requestScope: get value from the request field
      3. sessionScope: get the value from the session
      4. applicationScope: Get values ​​from the application (ServletContext)
    2. $ {Name} key: Omit the domain name

      The wording represents a turn from the smallest field to find out if there is a value corresponding to the key, until you find

    Question: How to get the value of non-string type?

    1. For values ​​of custom object type class: If direct access to the object, the value of the address is printed object. If you want the inside of the member variables, you want to get through the properties of the object (note the property rather than the member variables, of course, generally these two are the same).

      For example, a people class has a member variable for the String name, then define the setter and getter, the attribute name is name, then the property can be accessed using the $ {people.name}

      As another example, if we define a member variable of type Date in the class, which corresponds to a method for the getYear (), then the year is a property of the object, even though we have not defined in the class member variables of this year. At this point we can obtain the attribute value by $ {people.year}

      Because it calls essentially getter, rather than the member variables

      Similarly, if we have a custom property and set getter and setter, we can customize the output by setting the content of the getter

    2. For values ​​of type List:

      • Get a list:% {domain name key name.}, Thus obtained is [] it contains all the elements

      • Gets a specific element:% {domain name key name [index].}
      • If a subscript out of bounds, it returns an empty string, rather than an error

    3. Map for getting values ​​from the set of types of objects:

      • $ {Domain name. Keys .key name}
      • $ {Domain name. Key names [ "key name"]}

      Note that the first method of key name is not enclosed in double quotes, the second is added

note

  1. JSP EL expressions supported by default

    If we want to ignore EL expressions directly show the source text, you can set isELIgnored properties in the JSP is "true", so that the entire page EL expression failure as plain text.

    If you want to block a single EL expression, you can directly sign escape EL expressions: added before the EL expression \ represents the escape

  2. Implicit Objects

    No need to create an object can be directly used is called implicit object in EL

    The most commonly used is pageContext object, its features are:

    1. Gets JSP eight other built-in objects

      $ {PageContext.XXX} (because there pageContext getXXX method, so its properties XXX)

      For example: $ {pageContext.request.contextPath} to get the virtual directory, which is a dynamic access to project virtual directory on the server in the JSP page methods

      We recommend the place it comes to directories have a dynamic way to obtain virtual directory to use for the project in the deployment at different locations, for example:

      image-20191209155633227

Guess you like

Origin www.cnblogs.com/jiading/p/12014251.html