I want to pass an array of values that I have built within a JavaBean to a JSP, and for JavaScript within the page to process it and display it. The Bean sets the array, and the JavaScript has an identical array defined.

michael barbarino

I think you need to have a 'get' method in your bean to return the size of the array (if you don't have this already) and you will need to assign elements of the array individually, using an enumerator 'get' method, within a Javascript loop construct. Something like this:

myArray = new Array();
arraySize = <%= myJavaBean.getSize %>;
for (ix=0; ix < arraySize; ix++) 
  myArray[ix] = "<%= myJavaBean.getElement(ix) %>";
To do it the way you started, you would want to build something that looked like this, for example:
   var dayName = new Array ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
So your method 'getArray' would need to return the array initializer string:
javascriptArray = new Array(<%= myJavaBean.getArray() %>);
where 'getArray' returns a string containing:
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
(exactly as shown, with the quotation marks and commas).
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact