EL expression of JSP

1. Introduction to EL

  EL is one of the most important features of JSP2.0. Developers can use it to access application data, but not write operations. EL is designed to write concise JSP pages without the other three script elements of JSP. Readability is high.

  EL expressions start with "${" and end with "}", that is: ${expression}, if you want to use "${", you can use escape characters.

Second, . and [] operator

  [] and . operators have similar functions, [] is more commonly used, and . is more concise.

  ${object["propertyName"]}

  ${object.propertyName}

  Note: You can only use the [] operator if propertyName is not a valid Java variable name, e.g. host, variable name with -.

3. EL implicit objects

  It is known that nine built-in objects of JSP can be accessed by script in a JSP page, and eleven implicit objects can also be accessed by EL expressions.

  

  1. pageContext

    The pageContext object represents the javax.servlet.jsp.PageContext of the current JSP page, which contains nine built-in objects of JSP.

    The following lists the properties of request commonly used to obtain user and page information:

    

  2. initParam

    Used to get a context parameter value, configured in XML: 

    <context-param>

      <param-name>username</param-name>

      <param-value>chen</param-value>

    </context-param>:

    EL expression to get:

    ${initParam.username}

    ${initParam["username"]}

  3. param

    The implicit object param is used to obtain a request parameter, which is a Map type:

    ${param.userName}

    ${param["userName"]}

    Equivalent to

    ${pageContext.request.getParameter("userName")}

  4. paramValues

    The implicit object paramValues ​​can obtain multiple values ​​of a request parameter. This object includes all request parameters and uses the parameter name as a key. The value of each key is an array of strings, which includes all the values ​​for the specified parameter name:

    ${paramValues.students[0]} //The first value of the students array

  5. requestScope、sessionScope、applicationScope

    These implicit objects can be used to obtain variable values ​​in the request, session, and application scopes. For example, find the myVar property in the request scope: ${requestScope.myVar}, which is often used to access JavaBeans (see later).

    Objects with scope can also be accessed with an EL expression without a specified scope. In this case, the JSP container will return the specified object recognized for the first time in PageContext, ServletRequest, HttpSession, ServletContext, and press the button in EL. The order starts from the small range (request) to the largest range (application) until it is found, if not, it returns null, but the EL expression is optimized on the JSP page and does not display anything.

Fourth, access JavaBean

  You can use the . and [] operators with the specified scope to access the properties of a bean, such as accessing the value of the name attribute of the Student object in the session scope:

  ${sessionScope.Student.name}

  ${sessionScope["Student"]["name"]}

  注意:如果该属性是一个对象,那么仍然可以继续利用运算符访问第二个对象的属性;如果是一个Map或List,数组,则继续利用运算符访问,只不过这时后面应该用int型下标来表示Map,List,数组等指定下标的值,例如访问学生修的第一个课程名:${sessionScope.Student.courseName[0]}。

五、自动转型

  与JavaSE的语法不同,EL的自动转型:

  ${count + 1}

  如果count是”13“,那么最终结果是"14",这是因为count自动转型为int型再和1相加,并且实现从左到右的顺序,结果与在Java中的131区别开来。

六、算术,关系,逻辑,条件,empty运算符:

  在这里介绍一下条件运算符:

  ${statement? A : B}

  当statement返回真,则该EL表达式的值为A,否则为B。statement里也可用带有关系运算符或empty运算符的EL表达式。

  注意:如果你要用EL输出一个常量的话,字符串要加双引号,不然的话EL会默认把你认为的常量当做一个变量来处理。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324933617&siteId=291194637