Objective 2) EL, arrays and collections

7.2) Given a scenario, write EL code that uses the following operators: property access (the . operator), collection access (the [] operator).

Using dot and [] to access objects

The dot operator (.) is the default way of accessing objects within EL, thus I have shown how you can access param values with

${param.paramname}

But you can also access the map elements of implicit objects using square braces ([]), thus the first example given of

${param.username}

Can be reformulated as

${param["username"]}

Accessing Array elements

Square braces can of course be used to access elements of arrays but the EL goes further in its interpretation of array like expressions. And thus the following code will output the string cat six times.

<html>
<%
String [] noises = {"cat","dog","cow"};
request.setAttribute("noises",noises);
%>
${requestScope.noises[0]}
${requestScope.noises["0"]}
${requestScope.noises['0']}
${noises[0]}
${noises["0"]}
${noises['0']}
</html>

Note how the element offset can be accessed either with the naked number, with double quotes or with single quotes. The first three examples explicitly set the implicit object (requestScope) whereas the last three leave it undefined.

Remember that requestScope has a type of map and is not the same as the request implicit object available within JSP scriptlets. Thus the following script will work.

<%= request.getMethod() %>

But the following is not valid EL

${requestScope.getMethod}

It is possible to get access to the methods of the request via the pageContext. Thus the following code will access the HTTP method of the page.

${pageContext.request.method}


Pay close attention to EL use of maps and arrays, it is not the same as standard Java

Accessing a parameter array

If you have the same HTTP query value multiple times the values can be accessed through the EL via the paramValues array. Thus the following query string

index.jsp?productid=10&productid=11

Results in the EL having access to an array called product id with two elements. The values in this array can be accessed either via

${paramValues.productid[0]}
${paramValues.productid[1]}

or the equivalent

${paramValues['productid'][0]}
${paramValues['productid'][0]}

The second version would also work correctly with double quotes around productid.

Accessing Map and List elements

The EL can access Map and List objects in a similar way

<html>
<%
   java.util.HashMap noises = new java.util.HashMap();
   noises.put("cat","meeow");
   noises.put("dog","woof");
   noises.put("cow","moo");
   noises.put("human being","speech");
   request.setAttribute("noises",noises);
%>
${requestScope.noises.cat}
<!-- the scope can be center implicit -->
${noises.cat}
${requestScope.noises["cat"]}
<!--
square braces allows the use of names that
cannot be used for Java objects, i.e. with
 spaces or dots in them 
 -->
${requestScope.noises["human being"]}
</html>

The following code demonstrates how accessing the elements of a List is similar to access array elements.

<html>
<%
   /* accessing List elements with the EL */
   java.util.ArrayList noises = new java.util.ArrayList();
   noises.add("cat");
   noises.add("dog");
   noises.add("cow");
   noises.add("human being");
   request.setAttribute("noises",noises);
%>
<!-- note how bare numbers or numbers with quotes -->
${requestScope.noises[0]}
${noises["3"]}
</html>

You cannot use the dot notation with arrays or Lists, thus the following would cause a compile time error.

${noises.3}<!--will not compile -->

Accessing Bean Properties with EL

EL expressions can access properties of JavaBeans in a similar way to accessing elements of collections. Given the following code as a JavaBean.

package com.examulator;
public class Name {
   private String firstName, lastName;
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   public String getFirstName() {
      return firstName;
   }
    
   public String getLastName() {
      return lastName;
   }
}

The bean can be instantiated within a JSP and its values can be accessed using EL syntax as follows.

<html>
  <jsp:useBean id='name' class='com.examulator.Name'>         
         <jsp:setProperty name='name'
                      property='firstName' value='Marcus'/>
         <jsp:setProperty name='name'
                      property='lastName'  value='Green'/>
  </jsp:useBean>
  ${name["firstName"]}
  ${name['firstName']}
  ${name.firstName}
</html>

The bean is instantiated with the useBean tags, the properties are set with the setProperty tags. Note how the code uses three different ways of accessing the bean values. Behind the scenes the code is translated into a call to the getName method of the JavaBean. The output from this code will be the string Marcus output three times.


Other Sources

According to Mikalai Zaikin
http://java.boot.by/wcd-guide/ch07s02.html

Last modified: Sunday, 20 September 2015, 07:21 PM