• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing an array back from Javabean to JSP

 
Mike London
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a javabean which creates an array of objects. Eeach array element is just a simple class (object) with three strings filled in after a database query.
I want to pass the array back to the JSP page so I can loop though it and display the contents.
Although I can execute the javabean's "getter" method to return the array, I can't figure out how to create a variable type in the JSP page to HOLD the array so I can loop though it.
The type of the variable in the JSP page must be, I think, the same type as the array in the bean: "DBFields[]" --> (DBFields[] is the array of ojbects defined in the bean).
Unless I can actually return the whole array to a variable in the JSP page, I can't process it.
The JavaBean's ID is "SQLBean" and the array created in the javabean is DBFields[].
How can I return the array from the bean to a variable in the JSP page? I can't seem to get a variable to work without a Tomcat error saying it can't find the class, etc.
Thanks much for anyone's input!
Mike
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its a bit difficult to say without seeing your code. Its strange that your javabean can see the class and the jsp can't - its not an internal class is it? anyway, your claim that you cant process the bean without returning the array is not entirely accurate surely - it must be possible to have a currentObject property that can be set with a next() method so that you can access the objects through the javabean.
anyway, you shouldnt have to do that unless it makes sense, so:
did you import the class in page declaration?
is the class private to the same package as the bean or is it an internal class?
otherwise post your code
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this on your JSP page:

Now you can do this:


/Rene
 
Mike London
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, here's the actual JSP code in its entirety. I've also included the relevant bean code below the JSP code so the class is shown in its context.
Thanks in advance!
Again, my problem is that I can't create a variable (of type DBFields[]) to hold the array of objects passed back from the bean.
-----------------------------------------
Here's the JSP Code that is trying to access the get the DBFields[] array returned from the bean
<html>
<head>
<title>
DBAccess1
</title>
</head>
<jsp:useBean id="SQLData" scope="application" class="dbaccess.DBAccess1" />
<% SQLData.setTableName("Process"); %>
<% SQLData.setNameField("PRO_Name"); %>
<% SQLData.setPkFieldName("PRO_PK"); %>
</body>
<h1>
JBuilder Generated JSP
</h1>
Value of SQLData.nameField is :<jsp:getProperty name="SQLData" property="nameField" />
Value of SQLData.pkFieldName is : <jsp:getProperty name="SQLData" property="pkFieldName" /><br>
Value of SQLData.tableName is : <jsp:getProperty name="SQLData" property="tableName" /><br>
<hr><br>
Below does NOT WORK.
<%
DBFields[] fields = SQLData.getDBFields();
%>
<form method="post">
</html>
---------------------------------------------
Here's the relevant bean code:
package dbaccess;
public class DBAccess1
{
private String tableName;
private String nameField;
private String pkFieldName;
DBFields[] returnList;
private String SQLString;
DBFields currentRow;
private String sourceURL;
private String driverClass;
private String SQLQueryString = null;
public DBAccess1()
// constructors here...
// getter-setters
public DBFields[] getDBFields()
{
return this.returnList; //return the array
}
// code that extracts the database data and
// creates the array above (based on the class
// definition below is not shown for brevity.
} // end class DBAccess1

class DBFields
{
String DBfield1Data = null;
String DBfield2Data = null;
String DBfield3Data = null;
public DBFields()
{
}
public DBFields(String DBField1, String DBField2, String DBField3)
{
this.DBfield1Data=DBField1;
this.DBfield2Data=DBField2;
this.DBfield3Data=DBField3;
}
}
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike
Your DBFields class has default access, so unless the class DBAccess1 and the JSP are in the same package they wont be able to access the class. IF the DBFiels class is in the same file as the DBAccess1 clas then you'll have to pull it out and make it a public class in it's own file/compilation unit.
hope that helps
 
Mike London
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it working thanks to all the great replies on this forum!
Thank you all!!!
Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!