【发布时间】:2012-03-29 09:42:45
【问题描述】:
我正在尝试将数据从 spring 控制器传递到 javascript,但没有运气。我应该使用 ajax 来做到这一点吗?请你能给我一些关于如何做到这一点的提示吗?最好的方法是什么?
在我的控制器中,我尝试传递数据:
@RequestMapping(value = "/map", method = RequestMethod.GET)
public String map(ModelMap model) {
...
model.addAttribute("trackpoints", json);
return "map";
}
其中 json 是一个 gson 对象(JsonObject),包含:
{"trackpoints":[{"latitude":52.390556,"longitude":16.920295},
{"latitude":52.390606,"longitude":16.920262}]}
在我的 jsp 文件中,我有:
<script type="text/javascript">
var myJSON = {};
myJSON = ${trackpoints};
document.writeln(myJSON.trackpoints);
</script>
但结果是:
[object Object],[object Object]
我会更详细地解释这一点:>
我想使用谷歌地图 api 来显示地图并绘制从许多纬度、经度协调的路径。 我认为 json 会比 list 更好,但我可能是错的。
我尝试调整文档中的代码 - 在下面的代码中,我尝试用循环替换硬编码的坐标,以及来自 json 对象的值。
<script type="text/javascript">
function initialize() {
var myLatLng = new google.maps.LatLng(0, -180);
var myOptions = {
zoom : 3,
center : myLatLng,
mapTypeId : google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var flightPlanCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856),
new google.maps.LatLng(-18.142599, 178.431),
new google.maps.LatLng(-27.46758, 153.027892) ];
var flightPath = new google.maps.Polyline({
path : flightPlanCoordinates,
strokeColor : "#FF0000",
strokeOpacity : 1.0,
strokeWeight : 2
});
flightPath.setMap(map);
}
</script>
我希望现在更清楚:)
【问题讨论】:
标签: java javascript json spring