aspose file tools*
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
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
Author

EL to display bean attribute evaluates to null

venkatesh seshagiri
Greenhorn

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 );
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
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.

cheers,
ram.
venkatesh seshagiri
Greenhorn

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
Venkatesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EL to display bean attribute evaluates to null