• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

EL to display bean attribute evaluates to null

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 );
customerList.add(customer);
}
request.setAttribute("customers", customerList);
RequestDispatcher view = request.getRequestDispatcher("CustomerSelection.jsp");
view.forward(request,response);

Code in the JSp which tries to display the Customer Name

<table>
<c:forEach var="customer" items="${customers}" varStatus="loopCount">
<tr>
<td>
${loopCount.count}
</td>
<td>
${customer.customerName}
</td>
</tr>
</c:forEach>
</table>

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();
while(it.hasNext()){
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.

Thanks
Venkatesh
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.

cheers,
ram.
 
venkatesh seshagiri
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Venkatesh
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic