• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beans found - but can't display data! Please help...

 
Marcellus Tryk
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi -
I've looked thoughout this forum (and the servlets forum) to find a similar problem but no luck. I'm thinking it must be something obvious - but I just can't see it.
I'm sending an a array of CatBeans to my jsp and trying to display a property for each bean. I know the array is successfully retrieved because the text "A cat: " is displayed for each CatBean I send - but no value prints out for the catcode. I know the property has a value because I can display it using a scriptlet. But the jsp getProperty tag won't display it. Can anyone give me a clue? I'm flummoxed. Jsp code below.
Thanks,
Mars
<%@ page import="marcellus.quiz.*" errorPage="/jsp/error.jsp" %>
<jsp:useBean id="cat" class="marcellus.quiz.CatBean"/>
<%
CatBean[] cats = (CatBean[])request.getAttribute("cats");
%>
<html>
<head>
<title>Edit Categories</title>
</head>
<body>
<h2>Edit Categories</h2>
<table>
<% for (int i=0; i < cats.length; i++) {
cat = cats[i];
%>
<tr>
<td>
A cat: <jsp:getProperty name="cat" property="catcode"/>
</td>
</tr>
<% } %>
</table>

</body>
</html>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the problem lies in the fact that you have two cats. (Obvious jokes elided).
One is stored as an attribute in the request via your jsp:useBean tag, and the other is defined within the scope of your for loop.
Thus when you execute:

The cat retrieved from the cats array, and the cat referenced in the jsp:getProperty tag are different objects. In fact, unless you set a CatBean into the request as "cat" prior to getting to this page, what the jsp:getProperty is referencing is a CatBean constructed using the default constructor (which probably has no data in it?)
What I suspect you are trying to do can be solved by removing the jsp:useBean entirely (not needed unless you are trying to store an instance of CatBean in the request) and code your loop as:

or you could even factor out the need for the cat variable at all with:

hth,
bear
 
Marcellus Tryk
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear -
Thanks for you timely and helpful reply. Your response makes perfect sense, but I was confused because your analysis also applies to the source code I used as a model - from 'Web Development with Java Server Pages'. I just went to the online forums supporting the book and others have noticed that the book's code is incorrect. By the way, I think the book is excellant, other than this inaccuracy.
Thanks again!
Mars
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!