Javaweb-el-expression

Source: Internet
Author: User

El full name Expression Language, one of the most important features of jsp2.0, can use El expressions to access data in the application to eliminate Java scripts in JSP pages.

  • The syntax of an EL expression
    exPRessIoNExampleasx+y can beinWriteinto {X+y}

  • Keywords in El
    The following are the keywords in El that they cannot be used as identifiers:
    And,eq,gt,true,instanceof,or,ne,le,false,empty,not,lt,ge,null,div,mod
    The Department key words will be further described later.

  • operators [] and.
    An El expression can return a numeric value of any type. For example, the result of an El expression is an object with attributes, you can take advantage of [] and. To access the property values, the effect is exactly the same, except for the [] ratio. A little more formal.

    Eg: ${object.["propertyName"]} 或 ${object.propertyName}

    However, note that a very important "trap", when the property name is not a valid Java variable name, can only be used instead of []. Operation.
    Eg: Gets the value of the host property in the request header
    ${header["host"]} 或 ${header.host}It's no problem.

    However, when you want to get the Accept-language property, you can only use [].

     ${header["accept-language"]}
    当获取到的结果仍然是一个对象时,还可以继续用 [] 或 . 来获取其中的属性。例如,利用el表达式获取servlet path
    ${pageContext["request"]["servletPath"]}    ${pageContext.request["servletPath"]}    ${pageContext.request.servletPath}
    获取HttpSession中的属性    ${pageCotext.session}    ${pageContext.session.id}
  • The value rule for the
  • El expression
    El expression is taken from left to right, for expr-a[expr-b] forms of expression, the value rule is entered:
    1) Calculate expr-a First, get value-a
    2) If value-a is null, return NULL
    3) and then calculate expr-b, Get value-b
    4) If value-b is null, return NULL
    5) If value-a is map, Then see if Value-b is a key in the map, and if so, return Value-a.get (value-b), or null if not,
    6) If the value-a is a list, or an array, the following processing
    6. A) forces the value-b to int and throws an exception if the conversion fails.
    6.b) if Value-a.get (value-b) throws Indexoutofbandsexception or Array.get (value-a,value-b) If Arrayindexoutbandsexception is thrown, NULL is returned.
    6.c) Otherwise, if value is a list then return Value-a.get (value-b), if Value-a is an array, return Array.get (value-a,value-b)

    7) If Value-a is not a map,list or an array, then value-a must be a javabean, then the value-b must be forced to string. If Value-b is value-a is a readable property, call the Getter method.

  • The implicit object of the
  • El expression
    in the JSP page, you can take advantage of the JSP script method jsp implicit object. However, in a script-free JSP page, it is not possible to access these implicit objects, and the El expression accesses different objects by providing a set of their own implicit objects. The

    El expression is an implicit object that has:
    *pagecontext: It corresponds to the PageContext object in the JSP.

    Initparam: It contains all the environment initialization parameters, with the parameter name as the key's map

    *param: It contains all the request parameters, and the parameter name as a map of key, each key value is the first parameter value of the specified name.

    *paramvalues: It contains all the request parameters and a map with the parameter name key. The value of each key is an array of strings that contains all the parameter values for the specified parameter name.

    Header: This is a map containing the request title and using the title name as the key. The value of each key is the first heading that specifies the title name.

    Headervalues: This is a map that contains the request title and uses the title name as the key. The value of each key is an array of strings that contains all the parameter values for the specified caption name.

    *cookie: This is a Map,cookie name that contains all the cookie objects in the current request object, which is the name of the key, and each key is mapped to a cookie object.

    Applicationscope: This is a map containing all the properties of the ServletContext object, with the property name as key.

    Sessionscope: This is a map that contains all the properties of the HttpSession object and uses the property name as key.

    Requestscope: This is a map that contains all the properties in the current HttpServletRequest object and uses the name as the key.

    Pagescope: This is a map that contains all the properties of the full page range, with the property name as key.

Here's a quick introduction to these implicit objects.
1) PageContext
The implicit object corresponds to the PageContext in the JSP implicit object, which is the most commonly used value entry because it contains many other servlet-related objects, such as requst, response, out, session, application, Config, page, exception, and so on.

We can get the request object as follows

${pageContext.request}、${pageContext["request"]}

Gets the request method or one of the parameters in the request

${pageContext.request.method} ${pageContext["request"]["paramName"]}

An analogy ...

2) Initparam
The object contains the initial parameter value of the context, which is

<Context-param>           <Param-name>Contextconfiglocation</param-name>           <Param-value>Classpath:spring-beans.XMLClasspath:spring-CXF.XML           </param-value></context-param>

This is true if you want to use Initparam to get the value of this initialization parameter.

${initParam.contextConfigLocation}${initParam["contextConfigLocation"]}

3) param
This object is used to get request parameters, and if the client submits user name and password information to the server in a request, it can be obtained in the following form

${param.username}/${param["password"]}

4) Paramvalues
This object can be used to obtain multiple values for a request parameter, such as multiple check boxes, whose return value is an array of strings, and if we select more than one check box to illustrate hobbies, you can get these values in the following way

${paramValues.hobby[0]}  //第一个爱好${paramValues.hobby[1]}  //第二个爱好

5) Header
Also the Stealth object contains the request header, which you can use to get all the request header information

${header.connection}${header.host}${header["accept-language"]}

Note that, as mentioned earlier, if key is not a standard Java variable name, you can only use [] to get its corresponding value, just like accept-language.

6) Headervalues
Similar with paramvalues

${headerValues["accept-language"][0]}

7) Cookies
Use this implicit object to get the values of all cookies, such as
Get the Jsessionid data

${cookie.jsessionid.value}${cookie.jsessionid.path}

Get user session Data

${cookie.user.name}${cookie.user.age}

8) Applicationscope, Sessionscope, Requestscope, Pagescope
Each user obtains data from the ServletContext, Session, HttpServletRequest, and PageContext.

One point to note is, for example, to get the value of name with an El expression, ${name} so that the El will first find the value of the key name from the Pagescope range, and if not, it will follow the requestscope->sessionscope-> Applicationscope to look for it.

    • Other operators for El expressions

      1. Arithmetic operations
      · + Addition
      · -Subtraction
      · * Multiplication
      · /(DIV) Division
      · %/mod take-up/take-up mode

      2. Logical operators
      · &&/and and
      · || /OR OR
      · ! /Not non-

      3. Relational operators
      · = =/EQ equals
      !=/NE not equal to
      · >/GT Greater than
      · · >=/ge greater than or equal to
      · <=/lt less than or equal to
      · · <=/le less than or equal to

       4.三目运算符
    ${statement ? A : B}
        5.empty        用来检查一个值是否为null或空        ${empty x} 如果x为null或空(长度为0的字符串),则返回true,否则返回false

Not to be continued.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Javaweb-el-expression

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.