Expression Language - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Expression Language

Description:

Expression Language Lec - 42 Generating Dynamic Contents Technologies available Servlets JSP JavaBeans Custom Tags Expression Language (EL) JSTL JavaServer Faces ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 35
Provided by: ITSC99
Category:

less

Transcript and Presenter's Notes

Title: Expression Language


1
Expression Language
  • Lec - 42

2
Generating Dynamic Contents
  • Technologies available
  • Servlets
  • JSP
  • JavaBeans
  • Custom Tags
  • Expression Language (EL)
  • JSTL
  • JavaServer Faces (JSF)

3
  • Mike
  • Hard code developer
  • Handles all business logic
  • and backend matters
  • Expert in Java,
  • Database, XML etc.
  • Ernie
  • Jack of all trades
  • Not an expert in anything,
  • but will eventually get the job done.
  • Philippe
  • Web site designer
  • Knows how to make a
  • Website look really cool !
  • HTML / JavaScript expert

Credit Pierre Delisle (spec lead)
4
EL Purpose (ni)
  • Java as the scripting language in JSP scares many
    people (e.g. Philippe)
  • Can we simplify ?
  • Expression Language (EL)
  • A language adapted for the Web Developer

5
EL Benefits
Credit Pierre Delisle (spec lead)
6
EL overview (ni)
  • Not a programming or scripting language
  • Major goal Simplicity (and really is)
  • Inspiration from
  • JavaScript
  • XML path language (XPath)
  • Geared towards looking up objects their
  • properties and performing simple operation on
  • them

7
JSP Before EL
2. Must Know Type
1. Must Declare
  • lt
  • Person p (Person) request.getAttribute(
    person)
  • gt
  • .
  • Person Name lt p.getName() gt
  • lt if (p.getAddress( ).equals(defence) )
    gt
  • .
  • lt gt

4. Knowledge of Scripting Language required
even for simple manipulations
3. Awkward Syntax
8
JSP After EL
2. Easier syntax
1. Direct access
  • Person Name p.name
  • ltcif test p.address param.add gt
  • p.name
  • lt/cifgt

3. All app data easily accessible
4. Better adapted expression language
9
Expression Languagenuggets
10
EL nuggets
  • Expressions identifiers
  • Arithmetic, logical relational operators
  • Automatic type conversion
  • Access to beans, arrays, lists maps
  • Access to set of implicit objects servlet
    properties

11
EL Syntax
  • Format
  • validExpression
  • Valid Expressions
  • Literals
  • Operators
  • Variables (object references)
  • Implicit call to function using property name

12
EL literals
Literals Literal Values
Boolean true or false
Integer Similar to Java e.g. 243, -9642
Floating Point Similar to Java e.g. 54.67, 1.83
String Any string delimited by single or double quote e.g. hello , hello
Null null
13
EL literals (cont.)
  • Examples
  • false lt-- evaluate to false --gt
  • 83 lt-- evaluate to 24 --gt

14
EL Operators
Type Operator
Arithmetic - / (div) (mod)
Grouping ( )
Logical (and) (or) ! (not)
Relational (eq) ! (ne) lt (lt) gt (gt) lt (le) gt (ge)
Empty prefix operation to determine value is null or empty, returns boolean value
Conditional ?
15
EL Operators (cont.)
  • Examples
  • (65) 5 lt-- evaluate to 35 --gt
  • (x gt min) (x lt max)
  • empty name
  • Returns true if name is
  • Empty string (),
  • Null etc.

16
EL Identifiers
  • Identifiers
  • Represents the name of the object
  • Objects stored in JSP scopes (page, request,
    session, application) referred as scoped
    variables
  • EL has 11 reserved identifiers, corresponding to
    11 implicit objects
  • All other identifiers assumed to refer to scoped
    variables

17
EL Identifiers (cont.)Implicit Objects 1
Category Implicit Object Description
JSP pageContext used to access JSP implicit objects
Scopes pageScope A Map associating names values of page scoped attributes
Scopes requestScope A Map associating names values of request scoped attributes
Scopes sessionScope A Map associating names values of session scoped attributes
Scopes applicationScope A Map associating names values of page scoped attributes
18
EL Identifiers (cont.)Implicit Objects 2
Category Operator Description
Request Parameters Param Maps a request parameter name to a single String parameter value
Request Parameters paramValues Maps a request parameter name to an array of values
Request Headers header Maps a request header name to a single header value
Request Headers headerValues Maps a request header name to an array of values
19
EL Identifiers (cont.)Implicit Objects 3
Category Operator Description
Cookies cookie A Map storing the cookies accompanying the request by name
Initialization parameters initParam A Map storing the context initialization parameters of the web application by name
20
EL Identifiers (cont.)Implicit Objects
  • Examples
  • pageContext.response
  • evaluates to response implicit object of JSP
  • param.name
  • Equivalent to request.getParameter(name)
  • cookie.name.value
  • Returns the value of the first cookie with the
    given name
  • Equivalent to
  • if (cookie.getName().equals(name)
  • String val cookie.getValue()

21
Example Code Summation of two numbers using EL
  • netBeans project - elexample

22
EL Identifiers (cont.)Scoped Variables
  • Storing Scoped Varibales
  • HttpSession ses request.getSession(true)
  • Person p new Person()
  • P.setName(ali)
  • ses.setAttribute(person , p)
  • Person p new Person()
  • P.setName(ali)
  • request.setAttribute(person , p)

23
EL Identifiers (cont.)Scoped Variables
  • Storing Scoped Varibales
  • ltjspuseBean id class scope
  • ltjspsetProperty namep propertyname
    valueali/gt

24
EL Identifiers (cont.)(ni) Scoped Variables
  • Retrieving Scoped Variables
  • Any identifier (not an implicit object) is first
    checked against
  • page scope,
  • then request scope,
  • then session scope and
  • finally application scope
  • If no such variable is located in four scopes,
    null is returned

25
EL Identifiers (cont.)Scoped Variables
  • Search for a scoped variable, E.g. p.name

page scope
page scope
request scope
request scope
Searching Order
session scope
session scope
name
Found, Calls getName()
p
application scope
26
No slide insertion after
27
EL Accessors
  • The . operator let you access identifiers
    and their properties
  • Dot (.) operator
  • Typically used for accessing the properties of an
    object
  • Bracket ( ) operator
  • Typically used to retrieve elements of arrays
    collections

28
EL Accessors (cont.)
  • Dot (.) operator
  • Consider the expression person.name
  • The EL accesses object properties using the
    JavaBeans conventions (e.g. getName( ) must be
    defined)
  • If property being accessed itself an object, the
    dot operator can be applied recursively.E.g.
  • user.address.city

identifier
property
property of address
property object
identifier
29
EL Accessors (cont.)
  • Bracket ( ) operator
  • For arrays collections implementing List
    interface e.g. ArrayList etc.
  • Index of the element appears inside brackets
  • For example, personList2 returns the 3rd
    element stored in it
  • For collections implementing Map interface e.g.
    HashMap etc.
  • key is specified inside brackets
  • For example, myMapid returns the value
    associated with the id (key)

30
EL Robust Features
  • Multiple Expression can be combined mixed with
    static text
  • For example,
  • Hello user.firstName user.lastName
  • Automatic type conversion
  • EL can automatically wrap and unwrap primitives
    in their corresponding Java classes. E.g.
  • begin student.marks

Integer
int
behind the scenes
31
EL Robust Features (cont.)
  • No NullPointerException ?
  • If the object/identifier is null
  • For example person.name, e.g. if person is
    null
  • No exception would be thrown the result would
    also be null

32
Using ExpressionLanguage
33
Using EL Expressions
  • Can be used in following situations
  • As attribute values in standard custom
    actions. E.g.
  • ltjspsetProperty id person value . /gt
  • In template text the value of the expression is
    inserted into the current output. E.g.
  • lth3gt . lt/h3gt
  • With JSTL (discussed shortly)

34
Example Code Address book using EL
  • netBeans project - ExpressionLanguageExample
Write a Comment
User Comments (0)
About PowerShow.com