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

Like this? Share it with your network

Share

Unified Expression Language

  • 4,302 views
Uploaded on

Presentation about Unified Expression Language (Unified EL).

Presentation about Unified Expression Language (Unified EL).

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,302
On Slideshare
3,824
From Embeds
478
Number of Embeds
1

Actions

Shares
Downloads
113
Comments
0
Likes
1

Embeds 478

http://javaeecourse.devbg.org 478

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • 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 <jsp:useBean> and <jsp:getProperty>, a developer could simply use ${bean.property}

Transcript

  • 1. The Unified Expression Language Svetlin Nakov Borislava Spasova
  • 2. Contents
    • What is Unified Expression Language?
    • JavaBeans and JSP
    • EL and JavaBeans
    • The Unified EL
      • Scoped variables
      • Accessing beans and properties
      • Accessing collections
      • Operators
  • 3. 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}
  • 4. 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)
  • 5. JSP and JavaBeans Using JavaBeans in a JSP Page
  • 6. <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; />
  • 7. <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
  • 8. <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 ); }
  • 9. <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>
  • 10. <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; />
  • 11. The Unified Expression Language
  • 12. 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;/>
  • 13. 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 />
  • 14. 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]}
  • 15. 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}
  • 16. 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;}
  • 17. 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
  • 18. 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}
  • 19. 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() %>
  • 20. 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}
  • 21. 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}
  • 22. 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
  • 23. 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}
  • 24. Unified Expression Language in JSP and JSF
  • 25. 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
  • 26. 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}
  • 27. The Unified Expression Language (Unified EL)
    • Questions?
  • 28. 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.
  • 29. 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?