开发者

Passing a java array and use it in a spring view

I am having some troubles with how to pass variables in spring.

My intention is to pass an double array and to use this to create a google visualization by using the array in javascript.

The problem is that I only get the java object (toString), and I'm not able to loop over the values.

Java file.

double[] array = generateArray();

return new ModelAndView("array", "array", array);

And in my jsp

dataTable = new google.visualization.DataTable();
var data = '${array}';

dataTable.addColumn('string', 'Task');
dataTable.addColumn('number', 'Hours');
dataTable.addRows(data.length);

for (i=0;i<=data.length;i++)
{
    dataTable.setValue(i, data[i]);
}


var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240, is3D: true, title: 'My Array'});

What I want is to be able to loop over the array here. I know I could use Arrays.Tostring(开发者_如何学Go), but this seems unneccesary.


Using JSTL (requires taglib declaration <%@taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>):

var data = '<c:forEach var = "a" items = "${array}" varStatus = "s">${a}${s.last ? "" : ","}</c:forEach>';


The toString method of a double array returns something like this : [D@bfbdb0. So, you need to call a function which iterates on the array and prints out the array as a Javascript array.

Or you can do it with the JSTL :

var data = [<c:forEach var="d" items="${array}">${d}, </c:forEach>];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜