• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write retrieved data into javascript Array?

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
rick collette
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic