The moose likes JSP and the fly likes  EL to display bean attribute evaluates to null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark " EL to display bean attribute evaluates to null " Watch " EL to display bean attribute evaluates to null " New topic

EL to display bean attribute evaluates to null

venkatesh seshagiri

Joined: Jun 05, 2003
Posts: 7
Problem Description: Attribute value does not get displayed using EL in a JSP

JSTL tag used is c:forEach

A request from a html page is sent to servlet to get a page to select a customer for further processing. THe servlet populates an ArrayLIst with customer objects. Sets the arraylist to the request. Then forwards the request to a Jsp page. Inside the jsp page, JSTL c:forEach is used to display the customer name from the ArrayList which is already set in the request. I am getting the value for 'varStatus'<that is the serial number of each object in the array>. But i am unable to display the customer name using EL.

Mentioned below code snippet and the error message encountered.

Code in the servlet which forwards the request to a JSP page.

ArrayList customerList = new ArrayList(5);
for(int i = 0; i < 5; i++){
Customer customer = new Customer();
customer.setCustomerName("Name" + " " + i );
request.setAttribute("customers", customerList);
RequestDispatcher view = request.getRequestDispatcher("CustomerSelection.jsp");

Code in the JSp which tries to display the Customer Name

<c:forEach var="customer" items="${customers}" varStatus="loopCount">

When the jsp page is returned i get a ServletException saying that there is no value set for the attribute customerName in the Customer object.

In the above Jsp if i use ${customer[customerName]} instead of ${customer.customerName}, i do not get an exception but i get empty value in the browser. When i check in the view source, there is empty value between the TD.

Now i change the above code with scriplets as mentioned below, the customer name gets displayed.

ArrayList list = (ArrayList)request.getAttribute("customers");
Iterator it = list.iterator();
out.println("Customer Name --->" + ((Customer)it.next()).customerName());

What i do not understand is why it works for JSP scriplets and not for EL? Am i missing out anything here? Help in making me understand the issue would be helpful and thanks in advance for any inputs pertaining to the above mentioned problem.

ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

out.println("Customer Name --->" + ((Customer)it.next()).customerName());

Hmmm...obviously your accessor method (get() method) for the customerName attribute is customerName() where it should have been getCustomerName() for EL to work - you have to follow javabean naming conventions.

venkatesh seshagiri

Joined: Jun 05, 2003
Posts: 7
Yes i missed out adding 'get' to the accessor method<Being WebObjects developer got used to writing without get>.

With the suggested fix i get the customer name using EL.

Thanks for the inputs
It is sorta covered in the JavaRanch Style Guide.
subject: EL to display bean attribute evaluates to null
Similar Threads
Accessing an ArrayList of objects in JSP
JSTL forEach tag taking longer time than scriplets ???
EL in not working
Link to show another JSP with Detail from selected entry
JSTL <c:forEach .../> tag not working with collection