jsp:getProperty returning empty string

Hi All,

I have the following code:

<%@ page import="java.util.Vector, DTO.StaffDTO"%>

<html>

<head>

<title> Staff Details </title>

<script>

function SendToServer( pageIndex)

{

document.forms[0].action ="LoadRecords?page=" + pageIndex;

document.forms[0].submit();

}

</script>

</head>

<body>

<jsp:useBean id="myBean" scope="page" class="DTO.StaffDTO"/>

<form method="post">

<table border=1>

<tr>

<td>Employee ID</td>

<td>Name</td>

<td>Department</td>

</tr>

<%

Vector objResults = (Vector)request.getAttribute("Results");

objResults.trimToSize();

int ctr = 0;

while( ctr < objResults.size())

{

%>

<tr>

<td><jsp:getProperty name="myBean" property="name"/></td>

<td><jsp:getProperty name="myBean" property="customerID"/></td>

<td><jsp:getProperty name="myBean" property="balance"/></td>

</tr>

<%

ctr++;

}

%>

<%

int objTotalRecords = ((Integer)request.getAttribute("TotalRecords")).intValue();

%>

<tr>

<%

int pageIndex = 0;

while( (pageIndex * 10) <= objTotalRecords)

{

%>

<td align=right>

<a href="javascript:SendToServer(<%=pageIndex+1%>);"><%=pageIndex + 1%></a>

</td>

<%

pageIndex++;

}

%>

</td>

</tr>

</table>

</form>

</body>

</html>

What happens here is that <jsp:getProperty> tags return empty strings. I put some SOPs in the file where the values are extracted from the DB and put into the bean. The values are correctly retrieved and they are stored into the bean. Then what could be causing this problem?

[2800 byte] By [TopCodera] at [2007-10-3 4:52:23]
# 1
Toppy,I'm not certain, but I don't think you import your bean class... atleast I've found a couple of examples that don't...... also try just adding a getHelloWorld method to the bean which just returns "Hello World"... does that work?Keith.
corlettka at 2007-7-14 22:57:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
corlettk, the "Hello World" thing works, but I noticed something else too. I put SOPs inside the getter methods of the bean to output the value. They are returning null. Can anyone tell me why this is occurring?
TopCodera at 2007-7-14 22:57:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

What are you trying to achieve?

I see a loop iterating over a vector of results.

I see a bunch of getProperty statements.

The bit I'm not seeing on this page is any link between the loop, and your "myBean"

In fact I don't see anywhere you are referring to the current element of the vector you are iterating over.

The properties are null because you instantiate the bean, and never give them a value.

Suggestion: Use JSTL to iterate through this loop. It is much cleaner than using scriptlet/JSP mix.

<c:forEach var="result" items="${Results}">

<tr>

<td><c:out value="${result.name}"/></td>

<td><c:out value="${result.customerID}"/></td>

<td><c:out value="${result.balance}"/></td>

</tr>

</c:forEach>

evnafetsa at 2007-7-14 22:57:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

evnafets!! What I wanted to achieve was this: I have a table which contains three columns - name, customerId, balance. I wanted to display these values on the web page. I was able to do it using normal HTML and scriptlets, but since I am new to JSP, I thought I'd try them out using JSP actions too.

Yes, what you say is right. There is no link between the beans inside the Vector and the jsp action. But can you tell me how I can introduce this link?

TopCodera at 2007-7-14 22:57:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

I think you use an Iterator ... which gives you a "row" to play with.

something like

Iterator it = new Iteratator(ALMOST_ANY_COLLECTION)

while (it.hasnext()) {

print the row

it = it.next()

}

... and you may need some type casts in there but can't remember the exact syntax ... but I think it's something like Iterator <MYCLASS>it = new Iteratator <MYCLASS>(MYCOLLECTION)

... where the UPPERCASE stuff needs replacing with your names.

Hope that helps... but don't shoot me if it doesn't.

Keith.

corlettka at 2007-7-14 22:57:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

> Yes, what you say is right. There is no link between

> the beans inside the Vector and the jsp action. But

> can you tell me how I can introduce this link?

The problem here is that your bean is modelled to store exactly one Customer.

<jsp:useBean> works with exactly one bean object.

So ideally your Vector ought to be the 'bean' here. You iterate the Vector, each iteration returns the next Customer object and then you use <jsp:getProperty> to display each property.

However the things you can do with <jsp:useBean> is pretty limited. Its not possible to drill down into an object accessing nested object proprties. Its not also possible to access indexed properties.

So, use the jstl approach that evnafets suggested :).

<c:forEach var="result" items="${Results}">

<tr>

<td><c:out value="${result.name}"/></td>

<td><c:out value="${result.customerID}"/></td>

<td><c:out value="${result.balance}"/></td>

</tr>

</c:forEach>

The forEach loop iterates over the Vector, exposes each CustomerBean as a variable called 'result'.

Inside the loop ${result.name} in turn calls the getName() method on the bean (just like <jsp:getProperty). Same goes for all other properties.

ram.>

Madathil_Prasada at 2007-7-14 22:57:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Thank you RAM. I;m glad someone knows there sh1te
corlettka at 2007-7-14 22:57:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...