• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

unable to display bean property using ArrayList

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am new to JSP / EL.

I have a function to return an ArrayList of CategoryBean objects. When i tried to display in the JSP i get an exception.

The JSP code:
title>Welcome to Hbcdpm Store</title>
</head>
<body>

${categoryList[0].categoryId}



The Servlet code:

ArrayList<CategoryBean> results=categoryList.getCategoryName();
request.setAttribute("categoryList",results);
RequestDispatcher view = request.getRequestDispatcher("Main.jsp");
view.forward(request,response);



The exception was:

javax.servlet.ServletException: Unable to find a value for "categoryId" in object of class "au.org.hbc.main.CategoryBean" using operator "."


If i changed the JSP to display :

${categoryList[0]


then i get the output:
au.org.hbc.main.CategoryBean@9a9b65


Can someone please tell me what i have done wrong?

Thanks
 
Philip Tao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to include the CatalogBean code:


String getCategoryId() {
return category_id;
}

String getCategoryDescription() {
return category_description;
}
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

please, try to use JSTL <c:forEach> tag,
for example:


regards,
Natasza
 
Philip Tao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Natasza,

I copied the code to my jsp and i get the following error:

org.apache.jasper.JasperException: /Main.jsp(19,0) According to TLD or attribute directive in tag file, attribute items does not accept any expressions
 
Philip Tao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have gone through the JSTL installation i have and fix the taglib definition in the jsp.

Now i get a different error:

 
Philip Tao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I run tomcat 5.5.

My entire jsp code:




Web.xml




My Servlet:

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Philip,

it must be in our name ;) . I get exact the same error....

So, when I saw your post I hoped for an answer... but didn't find one yet. ;(

MyJSP:

<table>
<c:forEach var="location" items="${form.locations}">
<tr>
<td><c:out value="${location.locationId}"/></td>
<td><c:out value="${location.locationName}"/></td>
</tr>
</c:forEach>
</table>

result:
javax.servlet.ServletException: Unable to find a value for "locationName" in object of class "java.util.HashMap$Entry" using operator "."

and:

<td>${location.locationId}/></td>
<td>${location.locationName}/></td>

and

<td>${location["locationId"]}/></td>
<td>${location["locationName"]}/></td>

produce similar results


If I leave out the name/id thing and print ${location} I actually get the reference of the object I need (10=org.circe.web.model.Location@9b8ff9/>).I quess you get the same result. So the question is why, if we invoke another method on the bean then toString(), we get the error that the actual object can not be found?

Let me know if you found something, I will do the same

Philip.





Which produces
javax.servlet.ServletException: Unable to find a value for "locationId" in object of class "java.util.HashMap$Entry" using operator "."

 
Philip Plenckers
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Philip,

I guess in this case you have to write your own custom library tag. But as you I'm quite new to this issue.

But from p 438 of HeadFirst Servlets and JSP's I conclude that the <c:forEach> etc. is only capable of invoking the toString() method of the object in the Collection.
If this is enough you can simple override this method.
Maybe someone else can confirm this?

Philip
 
Natasza Biecek
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Philip & Philip,

Below you have my (working) vision of bean, servlet and jsp,
for ArrayList and HashMap both...

CatalogBean.java


catTestServlet.java


and catTestPage.jsp


Originally posted by Philip Plenckers:

MyJSP:
<table>
<c:forEach var="location" items="${form.locations}">
<tr>
<td><c:out value="${location.locationId}"/></td>
<td><c:out value="${location.locationName}"/></td>
</tr>
</c:forEach>
</table>
result:
javax.servlet.ServletException: Unable to find a value for "locationName"
in object of class "java.util.HashMap$Entry" using operator "."



Hmm.... HashMap is a collection of pairs... key + value
key identifies an entry, value - contains object...
when c:forEach iterates through collection, it extracts these pairs,
so, to access fields in your object you have to get the object first via '.value'
Please, look at the second part of processRequest() method in code above...

I need a cup of coffee...


regards,
Natasza

ps.
my current environment is: NetBeans 5.0 (bundled Tomcat 5.5.9), JDK 1.4.2, JSTL 1.1
 
Philip Plenckers
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Natasza,

absolutely great!!!

thanks a lot,

Philip (second)
 
Philip Tao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Natasza,

Wow! Thanks for the example. It has helped me alot.

At first finding my mistakes was quite hard even though i have your working copy to compare with.

I have discovered the following problems with my code and my setup:

1. I have wrong jstl libraries in the WEB-INF/lib directory. I have a extra library standard-1.0.2.jar and also standard.jar.
2. My java bean class CategoryBean has default access instead of public access (i don't know how i didn't pick it up after checking my code so many times).


I am jumping for joy now too!!


Once again thanks for spending time answering my question.


Regards,

Philip Tao
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic