Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing array from processAction to jsp

 
Susan Mincey
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a couple of days of frustration into this so I am asking for help. I have a portlet and the view.jsp has an option for the user to add a new record (which works fine) or to search the DB for existing records. The user selects from a dropdown and presses Search. This is picked up by processAction which sends it to the doGetLessonsList method. After grabbing the search parameter from the form, a db method is called that completes the search, creates and returns an arraylist of object, Lesson. Now I want to send that arraylist back to the jsp for display. So I planned to use the setRenderParameter for this. I had to convert the arraylist of Lesson objects to a String array and that's where it shuts down. I have tried 3 different methods of converting my arraylist and nothing has worked. The 3 different tries are commented out in the code below. It appears to be ArrayStoreException error.

The arraylist isempty test comes back False and the number of records comes back as 2 so I know the arraylist (lArray) is populated.

Here's my code - if anyone can provide some input, I would appreciate the advice or corrections.

private void doGetLessonsList (ActionRequest request, ActionResponse response)
throws PortletException, IOException
{
// get the search parameter from the form
String sSearchString = request.getParameter("searchRO");

try
{
ArrayList lArray = new ArrayList();


// call the method to perform the search and return as Arraylist
lArray = dbInterface.getLessonsList(sSearchString);
//lets test to see if anything is there
boolean arraytest = lArray.isEmpty();
String s = String.valueOf(arraytest);
int x = lArray.size();
String t = Integer.toString(x);

// convert arraylist to array as required by set.renderParameter


//neither of these 3 methods work!!
//String sArray [] = new String [x];
//lArray.toArray (sArray);

//String[] sArray = (String[])lArray.toArray(new String[lArray.size()]);

//String[] sArray = (String[]) lArray.toArray(new String[0]);



// get the size of the array so we can pass this also
// but convert to string first
//int arraysize = java.util.Arrays.asList(sArray).size();
//String val = String.valueOf(arraysize);

// make 3 items available to view.jsp
response.setRenderParameter("ArrayEmpty", s);
response.setRenderParameter("ArraySize", t);

//response.setRenderParameter("ArrayResults", sArray);


}
catch (Exception e)
{
e.printStackTrace();
}
}
 
Ming Chang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you run the line:

lArray.toArray (sArray);

and get the exception: ArrayStoreException, it is indicating object type in sArray are not the same type (or are not the super type) as the objects in lArray.

It is failing here because lArray contains object of class eg. XYZ, while sArray contains String.

In short, you can only perform toArray if both lArray and sArray will contain objects of the same type.

To achieve what you want in this case, you are probably better off iterating through each element in lArray, and then copy the toString() value into the sArray.

Hope this helps.

Ming
[ November 11, 2007: Message edited by: Ming Chang ]
 
Susan Mincey
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ming,
I solved the problem but I changed what I pass. I felt that at some point I could have a really really big array of results, so I now do a query for only the ID numbers of the records and return that in the list array.

Then I convert the list array to a string buffer. Then I convert the string buffer to a string and the string is what I put into set.RenderParameter. In the view.jsp, I grab that string, parse it and use the ID numbers to do another sql query and return the results directly to the view.jsp. It might not be the recommended procedure for JSR168 but it does work.

If anyone comes up with a better way, I'm willing to give it a try. Thanks

Susan
 
Sanjay Kannan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doing any DB operation in jsp is not a good solution. The other alternative for you would be to construct the string with some delimiter from the resultset in the action phase and build the string array and set in the renderresponse. In the view jsp retrieve the string array and parse the delimited data and display in the jsp

e.g. the data can be

bookid | bookdesc | bookamt

parse the delimited string and display in the jsp
 
Ming Chang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Susan,

Perhaps an alternate strategy for handling the action and passing the data to JSP is:

1. processAction() detects which option the user chose. Then passes a parameter to doView() method through setRenderParam()

2. in doView(), perform the database query.

3. At end of doView(), set the objects from DB into request, so your JSP can retrieve them.

This way your JSP won't be doing SQL queries.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic