Unified Expression Language

4,270
-1

Published on

Presentation about Unified Expression Language (Unified EL).

1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
4,270
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
133
Comments
1
Likes
2
Embeds 0
No embeds

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}
  • Unified Expression Language

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

    ×