我正在尝试查看地图上存储在postgreSQL数据库中的一些对象。使用的框架是spring和hibernate,前端有angular.js。我已经检查了来自hibernate的返回对象的控制类,服务类。在班级他们很好。但是当我检查js(angular.js)时,它有对象和数字。由于发生错误,因为代码期望对象而不是数字。返回对象与此类似。
Array[5]
0:Object
1:20
2:Object
3:24
4:13
length:5
Array包含从控制器类返回的结果。在地图上查看第一个对象。因为第二个是数字而不是对象,所以它会产生错误。 这是将对象返回到angular.js脚本的代码块。
@RequestMapping(value = "/crimerecode/getall.htm", method = RequestMethod.GET)
@ResponseBody
public List<CrimeRecord> getAllCrimeRecords(@RequestParam String startDate,@RequestParam String endDate) {
try {
// crds.invalidateCrimeRecord(cr.getGcrno());
System.out.println("=========controller===============");
for(int i=0;i<(crds.getAll(startDate, endDate)).size();i++){
System.out.println(((crds.getAll(startDate, endDate)).get(i).getTheGeom()));
}
return crds.getAll(startDate, endDate);
}
catch (ParseException ex) {
java.util.logging.Logger.getLogger(CrimeRecordController.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
这是角度脚本中的代码块...
$http.get('/CMISystem/crimerecode/getall.htm?startDate=' + $scope.startDate + '&endDate=' +
$scope.endDate
).
success(function(data) {
console.log(data);
alert(data);
data.forEach(function(value) {
console.log(value);
var geo = value.theGeom.geo;
var point = new OpenLayers.Geometry.Point(geo.lon, geo.lat);
point.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
var pointFeature = new OpenLayers.Feature.Vector(point, value);
.....
}
我只在这里发布了相关的代码片段和javascript ...
答案 0 :(得分:0)
我认为问题出在您从List<CrimeRecord>
方法返回的getAllCrimeRecords
。
尝试创建数组列表并将每个CrimeRecord
对象添加到数组列表中,如以下示例所示。
public List<CrimeRecord> getAllCrimeRecords(@RequestParam String startDate, @RequestParam String endDate) { List<CrimeRecord> cr = new ArrayList<CrimeRecord>(); try { for (int i = 0; i < (crds.getAll(startDate, endDate)).size(); i++) { cr.add(crds.getAll(startDate, endDate).get(i)); } } catch (ParseException ex) { java.util.logging.Logger.getLogger(CrimeRecordController.class.getName()).log(Level.SEVERE, null, ex); } return cr; }