Passing java array and using it in spring view

I am having some problems with how to pass variables to spring.

I intend to pass a double array and use it to create a google visualization using an array in javascript.

The problem is that I only get the java (toString) object and I cannot iterate 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'});

I want to be able to iterate over an array here. I know I can use Arrays.Tostring (), but that seems unnecessary.

+3
source share
2 answers

Using JSTL (taglib declaration required <%@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>';
+2
source

toString - : [D@bfbdb0. , , Javascript.

JSTL:

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

Source: https://habr.com/ru/post/1792184/


All Articles