Jakarta Expression Language

Problem

  • Сложно работать с JSP и даже JSTL когда нужно:

    • получать данные из объекта

    • выполнять примитивные операции

Solution

  • Jakarta Expression Language

  • EL

  • Ранее: Unified Expression Language

Java EE Web history

Java for Web history

Intro

Intro

  • Специальный язык, позволяющий максимально просто вставлять в страницу JSP результаты выражений

${ выражение }
${ requestScope.book.name }

Объекты в EL

Объекты области видимости

  • applicationScope

  • sessionScope

  • requestScope

  • pageScope

Дополнительные объекты

  • param

  • header

  • cookie

  • initParam

  • paramValues

  • headerValues

  • pageContext

Операторы EL

Property Access Operator

  • Property Access Operator

  • Он же .

${firstObj.secondObj}
${requestScope.employee.address}

Collection Access Operator

  • Collection Access Operator

  • Он же []

${myList[1]} and ${myList["1"]}
${myMap[expr]}
${myMap[myList[1]]}
${requestScope["foo.bar"]}

Операторы отношения

  • == (eq)

  • != (ne)

  • < (lt)

  • > (gt)

  • <= (le)

  • >= (ge)

Арифметические операторы

  • +

  • -

  • *

  • / (div)

  • % (mod)

Логические операторы

  • && (and)

  • || (or)

  • ! (not)

  • Оператор empty (проверка на null или пустое значение/пустой массив)

Operators Priority

  • [] .

  • () - изменения приоритета операторов

  • - (unary) not ! empty

  • * / div % mod

  • + - (binary)

Operators Priority

  • < > <= >= lt gt le ge

  • == != eq ne

  • && and

  • || or

  • ? :

Reserved Words

Reserved Words

  • and

  • or

  • not

  • eq

  • ne

  • lt

  • gt

  • le

Reserved Words

  • ge

  • true

  • false

  • null

  • instanceof

  • empty

  • div

  • mod

Examples

Examples

${ not empty ob and empty ob.text }
${ 1 > (7/3) } = false
${ 7.0 >= 5 } = true
${ 'Z' < 'a' } = true
${ 'dog' gt 'doc' } = true
${ 7.0E3 + 1.4 } = 7001.4
${ 17 mod 7 } = 3

Example

<%@ page
    language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"
%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP EL Example Home</title>
</head>
<body>
    <%
        List<String> names = new ArrayList<String>();
        names.add("Pankaj");names.add("David");
        pageContext.setAttribute("names", names);
    %>
    <strong>Simple . EL Example:</strong> ${requestScope.person} <br><br>
    <strong>Simple . EL Example without scope:</strong> ${person} <br><br>
    <strong>Simple [] Example:</strong> ${applicationScope["User.Cookie"]} <br><br>
    <strong>Multiples . EL Example:</strong> ${sessionScope.employee.address.address} <br><br>
    <strong>List EL Example:</strong> ${names[1]} <br><br>
    <strong>Header information EL Example:</strong> ${header["Accept-Encoding"]} <br><br>
    <strong>Cookie EL Example:</strong> ${cookie["User.Cookie"].value} <br><br>
    <strong>pageContext EL Example:</strong> HTTP Method is ${pageContext.request.method} <br><br>
    <strong>Context param EL Example:</strong> ${initParam.AppID} <br><br>
    <strong>Arithmetic Operator EL Example:</strong> ${initParam.AppID + 200} <br><br>
    <strong>Relational Operator EL Example:</strong> ${initParam.AppID < 200} <br><br>
    <strong>Arithmetic Operator EL Example:</strong> ${initParam.AppID + 200} <br><br>
</body>
</html>

Example

EL example