Unified Expression Language
Upcoming SlideShare
Loading in...5
×
 

Unified Expression Language

on

  • 4,089 views

Presentation about Unified Expression Language (Unified EL).

Presentation about Unified Expression Language (Unified EL).

Statistics

Views

Total Views
4,089
Views on SlideShare
3,617
Embed Views
472

Actions

Likes
1
Downloads
108
Comments
0

1 Embed 472

http://javaeecourse.devbg.org 472

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • SPEL – Simplest Possible EL SEL – Simple EL JSP EL не е подходящ - I mmediate evaluation , read-only Влиза в JSP - JSP EL или SEL (JSP 2.0) JSP's EL made retrieving values and resolving variables easier. Rather than using and , a developer could simply use ${bean.property}

Unified Expression Language Unified Expression Language Presentation Transcript

  • The Unified Expression Language Svetlin Nakov Borislava Spasova
  • Contents
    • What is Unified Expression Language?
    • JavaBeans and JSP
    • EL and JavaBeans
    • The Unified EL
      • Scoped variables
      • Accessing beans and properties
      • Accessing collections
      • Operators
  • What is Unified EL?
    • Unified EL is expression language for accessing objects in JSP and JSF
      • Provides access to JavaBeans objects and their properties
        • Get values, set values, access methods
      • Can access arrays, lists and maps
      • Example:
    ${some Bean .someProperty}
  • History of the EL
    • Starts from JSTL EL and SPEL
      • JSTL 1.0
    • Standard part of JSP 2.0
      • JSP EL
    • Problems with JSF (Faces 1.0)
      • JSP EL is weak for JSF
      • JSF EL is created
    • Unification in UEL (JSP 2.1 and JSF 1.2)
  • JSP and JavaBeans Using JavaBeans in a JSP Page
  • <jsp:useBean>
    • <jsp:useBean> l ets you load in a JavaBean to be used in the JSP
      • L ets you exploit the reusability of Java classes
      • S yntax:
      • After having the JavaBean instance you can modify its properties with:
        • <jsp:setProperty>
        • <jsp:getProperty>
    <jsp:useBean id=&quot;name&quot; class=&quot;package.class&quot; scope=&quot;page | request | session | application&quot; />
  • <jsp:useBean> and Scope
    • The scope of the beans specifies where the bean should be stored
      • page scope – the bean is available to the current JSP page only
      • request scope – the bean is available to all pages that take part in the processing of the current request
      • session scope – the bean is stored in the client's session
      • application scope – the beans is stored globally, available to whole the application
  • <jsp:useBean> – Example
    • Declaring a bean ( scoped variable ), available during the whole client's session
      • This is compiled to this code:
    <jsp:useBean id=&quot;user&quot; class=&quot;beans.User&quot; scope=&quot;session&quot; /> beans. User user = (beans. User ) session.getAttribute(&quot; user &quot;); if ( user == null) { user = new beans. User (); session.setAttribute(&quot; user &quot;, user ); }
  • <jsp:setProperty>
    • <jsp:setProperty>
      • Assigns a value to given property of given bean
      • When used inside <jsp:useBean> is executed only when the new object was instantiated
    <jsp:setProperty name=&quot;user&quot; property=&quot;firstName&quot; value=&quot;Nakov&quot; /> <jsp:useBean id=&quot;user&quot; class=&quot;beans.User&quot; scope=&quot;page&quot; /> <jsp:setProperty name=&quot;user&quot; property=&quot;firstName&quot; value=&quot;Nakov&quot; /> </jsp:useBean>
  • <jsp:getProperty>
    • <jsp:getProperty>
      • Retrieves the value of a bean property
      • Converts it to a string
      • Inserts it into the output
    <jsp:useBean id=&quot;user&quot; ... /> ... User login : <jsp:getProperty name=&quot;user&quot; property=&quot; login &quot; /> User home page: <jsp:getProperty name=&quot;user&quot; property=&quot;home P age&quot; />
  • The Unified Expression Language
  • Why Do We Need EL?
    • EL simplifies the development
      • Consider we have the following bean:
      • We can simplify this expression
      • this way:
    <jsp:useBean id=&quot;some Bean &quot; class =&quot;somePackage.someClass &quot; scope=&quot;request, session, or application&quot;/> ${some Bean .someProperty} <jsp:getProperty name=&quot;some Bean &quot; property=&quot;someProperty&quot;/>
  • EL and JavaBeans – Example <% String name = &quot;Svetlin Nakov&quot;; pageContext.setAttribute(&quot;name&quot;, name); %> Name: ${name} <br /> <% User user = new User( &quot;snakov&quot;, &quot;Svetlin&quot;, &quot;Nakov&quot;); pageContext.setAttribute(&quot;user&quot;, user); %> User first name: ${user.firstName} <br /> User last name: ${user['lastName']} <br /> User login: ${user[&quot;login&quot;]} <br />
  • Advantages of the EL
    • Concise access to a scoped variables (in the page , session , application , etc.)
      • To output a scoped variable named saleItem , you can use:
    • Simple access to collection elements
      • To access an element of an array, List , or Map , you can use:
    ${saleItem} ${collectionObject[indexOrKey]}
  • Advantages of the EL (2)
    • Shorthand notation for bean properties
      • To output the companyName property of a scoped variable named company , you use:
      • To access the firstName property of the president property of a scoped variable named company , you use:
    ${company.companyName} ${company.president.firstName}
  • Advantages of the EL (3)
    • Fast access to request parameters, cookies, headers and other request data
    • Useful set of simple operators
      • + , - , * , / , < , > , == , && , || , empty , ?:
    ${param[&quot;username&quot;]} ${cookie[&quot;dateOfLastVisit&quot;]} ${ ( 2 + 5 ) * 3 } ${visitors. t otal C ount - 1} ${empty userBean}  true / false ${empty userBean ? &quot;N/A&quot; : &quot;user found&quot;}
  • Advantages of the EL (4)
    • Automatic type conversion
      • Data is automatically converted from and to String values
    • Empty values instead of error messages
      • Missing values or NullPointerException result in empty strings, not thrown exceptions
      • This could be a problem, especially during the debugging
  • How Scoped Variables Are Accessed?
    • Consider the following expression:
    • The EL evaluator performs search for the key &quot; someBean &quot; in these contexts:
      • pageContext ( PageContext )
      • request ( HttpServletRequest )
      • session ( HttpSession )
      • application ( ServletContext )
    • Search is done in the given order
    ${someBean}
  • How Bean Properties Are Accessed?
    • Consider the following expression:
      • It finds scoped variable of given name and outputs the specified bean property
    • Equivalent to the following code:
    ${customer.firstName} <%@ page import=&quot;beans.Customer&quot; %> <% Customer customer = (Customer) pageContext.findAttribute(&quot;customer&quot;); %> <%= customer.getFirstName() %>
  • How Collections Are Accessed?
    • Accessing collection entries:
    • For Array is equivalent to:
      • theArray[index]
    • For List is equivalent to:
      • theList.get(index)
    • For Map is equivalent to:
      • theMap.get(keyName)
    ${someCollection[entryName]} ${someCollection . entryName}
  • Referencing Implicit O bjects
    • pageContext – the objects in the PageContext
      • ${pageContext.session.id}
    • param – request parameters
      • ${param.custID}
    • header – HTTP request headers
      • ${header[&quot;Accept-Encoding&quot;]}
    • cookie – cookie objects (not cookie values)
      • ${cookie[&quot;userCookie&quot;].value}
    • initParam – context initialization param
    • pageScope , requestScope , sessionScope , applicationScope – directly accessing scopes
      • ${pageScope.user.firstName}
  • EL Operators
    • Arithmetic operators
      • + - * / div % mod
    • Relational operators
      • == eq != ne < lt > gt <= le >= ge
    • Logical operators
      • && and || or ! not
    • Empty check operator
      • empty <object>
      • Returns true for null, empty string, empty array, empty list, empty map
  • Conditional Evaluation
    • Evaluates condition and outputs either expression1 or expression2
    • Problems:
      • Relatively weak
        • c:if and c:choose from JSTL are much more powerful
      • Tempts you to put business (processing) logic in the JSP page
        • Should be used for presentation logic only
    ${condition ? expression1 : expression2}
  • Unified Expression Language in JSP and JSF
  • Types of Expressions
    • Unified EL is supported since In JSP 2.1 and JSF 1.2
      • It defines value and method expressions
    • Value expressions
      • Lvalue expressions – read only
      • Rvalue expressions – read/write
    • Method expressions
      • Return method reference that can be called at run-time
  • Immediate and Deferred Evaluation
    • Immediate evaluation
      • Evaluates the expression and return a value at the time the page is rendered
      • Used for lvalue expressions only
    • Deferred evaluation
      • Evaluates the expression and return a reference to the object containing the value
      • Can be used as lvalue and rvalue expression
    ${user.firstName} # {user.firstName}
  • The Unified Expression Language (Unified EL)
    • Questions?
  • Problems
    • Create a JavaBean Student that holds the information about an university student (name, address, email, faculty number, course, specialty, university).
    • Create a HTML form for entering student data and a servlet for processing the form data. The servlet should store the data as a Student bean in the session.
    • Create a JSP page that displays the student data from the session. Use the Unified EL.
  • Homework
    • Modify the previous application to hold in the session a list of students. It should allow adding new student and listing all students.
    • How you can implement the loop through the students in combination with EL?