aspose file tools*
The moose likes Struts and the fly likes How to write retrieved data into javascript Array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to write retrieved data into javascript Array?" Watch "How to write retrieved data into javascript Array?" New topic
Author

How to write retrieved data into javascript Array?

rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
Hi, guys:
I can use JavaScript array of arrays such as the
following to fill a box.

products = new Array(
new Array(
new Array("Milk", 1),
new Array("Meat", 2),
new Array("Vegetables", 3)
),
new Array(
new Array("Allergy", 1),
new Array("Cold", 2),
new Array("Sinus", 3)
),
new Array(
new Array("Women", 1),
new Array("Men", 2),
new Array("Kids", 3)
)
);

The above Array is used as a JavaScript function parameter. When I use a collection or an ArrayList ofArrayLists to replace the above JavaScript Array, it fails to show up in the box. I am wondering if there is a way to retrieve data using scriptlet and put data into javascript Arrays, because I need to fill the box using dynamic data. Or any other way to handle that? Thanks in advance.
[ July 25, 2003: Message edited by: rick collette ]
rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
Hi,
I really need to solve this as soon as possible. I really appreciate your help if you know how to handle this.
I am trying to retrieve data from database and pass
data into a JavaScript function as an Array and
display it on my JSP page.
If I have to write a Scriptlet inside JavaScript code
to get data from db, what kind of Java data form
(ArrayList, Vector, Enemeration, List, Collection?) I
can use to directly pass it into javascript function?
If the above cannot work, How can I transform a Java
array (a array of strings) into a JavaScript Array?
Thank you in advance.
regards,
rick
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

In the JSP, JavaScript code is just text that goes straight into the response. So, you can write a loop by using either a scriptlet or logic:iterate tag to generate it. For example,

[ July 25, 2003: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
Thanks for your nice hint:
I did the following, but the results were not displayed:
products is a collection of ArrayLists, every arrayList contains
a list of ProductBeans, ProductBean has productName and productId properties.
products are retrieved in action class and are put into session scope:
session.setAttribute("products", products);

var productsArray = new Array();
<logic:iterate id = "aList" name = "products" indexId = "idx">
<logic:iterate id = "ProductBean" name = "aList" indexId = "index">
productsArray[<%=idx%>][<%=index%>]
= new Array(<bean:write name = "ProductBean" property = "productName"/>,
<bean:write name = "ProductBean" property = "productId"/> ;
</logic:iterate>
</logic:iterate>


One extra question, do products collection need to conform to JavaBean convention for doing this kind of task?
I appreciate your help.
[ July 25, 2003: Message edited by: rick collette ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

yes, the objects used by the bean tags should have correspondingly named getters and setters.
Try looking at the source of the generated HTML that comes back and see if there are any syntax errors in the JavaScript. If the JavaScript is OK, then you might need to put a call to the JavaScript function in your body tag's onload event.
rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
Hi,
The JavaScript function is working. The only thing that will go wrong is this piece of code.
Seems logic tags also should have corresponding beans with getters/setters according to Programming Jakarta Struts book (p212).
If property is specified for a <logic:iterate> tag,
the corresponding getter will be called. I followed that example.
But I got the following error now:
javax.servlet.ServletException: Cannot find bean ProductBean in any scope
products were put into session scope in action, but ProductBean was never put in any scope. How is that happening? How to put ProductBean into scope?
Thanks in advance.

Originally posted by Junilu Lacar:
yes, the objects used by the bean tags should have correspondingly named getters and setters.
Try looking at the source of the generated HTML that comes back and see if there are any syntax errors in the JavaScript. If the JavaScript is OK, then you might need to put a call to the JavaScript function in your body tag's onload event.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write retrieved data into javascript Array?