jQuery in Action, 2nd edition*
The moose likes JSP and the fly likes unable to display bean property using ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "unable to display bean property using ArrayList" Watch "unable to display bean property using ArrayList" New topic
Author

unable to display bean property using ArrayList

Philip Tao
Greenhorn

Joined: Feb 08, 2006
Posts: 7
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

Joined: Feb 08, 2006
Posts: 7
I forgot to include the CatalogBean code:


String getCategoryId() {
return category_id;
}

String getCategoryDescription() {
return category_description;
}
Natasza Biecek
Greenhorn

Joined: Oct 26, 2006
Posts: 28
Hi,

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


regards,
Natasza
Philip Tao
Greenhorn

Joined: Feb 08, 2006
Posts: 7
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

Joined: Feb 08, 2006
Posts: 7
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

Joined: Feb 08, 2006
Posts: 7
I run tomcat 5.5.

My entire jsp code:




Web.xml




My Servlet:

Philip Plenckers
Greenhorn

Joined: Jul 25, 2006
Posts: 18
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><cut value="${location.locationId}"/></td>
<td><cut 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
Philip Plenckers
Greenhorn

Joined: Jul 25, 2006
Posts: 18
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

Joined: Oct 26, 2006
Posts: 28
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><cut value="${location.locationId}"/></td>
<td><cut 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

Joined: Jul 25, 2006
Posts: 18
Hi Natasza,

absolutely great!!!

thanks a lot,

Philip (second)
Philip Tao
Greenhorn

Joined: Feb 08, 2006
Posts: 7
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to display bean property using ArrayList