我正在尝试使用Kendo UI Grid来填充数据库中的数据。我正在创建的应用程序是使用Spring 3.2,Hibernate4和Jackson Fasterxml。我知道我的控制器正在返回JSON。我知道这一点,因为当我点击为控制器调用的URL时,我得到以下JSON响应:
[{"applicationId":11,"applicationName":"LOS","url":"test.com","serverId":1,"serverName":"vmlosweb01","serverIp":"10.49.10.89","createdDate":1373644385213,"modifiedDate":null,"genericUserName":"lsdefault","genericPassword":"password1","orgId":null,"environmentId":1,"environmentName":"SANDBOX","createdDate":1373904291147,"modifiedDate":null,"databaseInfoId":1,"databaseName":"SANDBOX Database","ipAddress":"10.49.10.145","environmentId":1,"environmentName":"SANDBOX","createdDate":1373904291147,"modifiedDate":null,"createdDate":1373904415710,"modifiedDate":null,"createdDate":1374169441500,"modifiedDate":null}]
这是我的代码:
appinfo.js
$(document).ready(function () {
var dataSource = new kendo.data.DataSource({
transport: {
read: {
read: "/appinfo/findApplications",
dataType: "jsonp"
}
},
pageSize: 10
});
dataSource.fetch(function(){
var data = this.data();
console.log(data.length);
});
$("#applicationsGrid").kendoGrid({
dataSource: dataSource,
pageable: true,
height: 600,
scrollable: true,
sortable: true,
filterable: true,
columns: [
{field: "applicationName", title: "Application Name"},
{field: "url", title: "URL"},
{field: "serverId", title: "Server"},
{field: "environmentId", title: "Environment"},
{field: "databaseInfoId", title: "Database"},
{field: "genericUserName", title: "Default Username"},
{field: "genericPassword", title: "Default Password"}
]
});
});
ApplicationInformationController.java
package com.lps.appinfo.controller;
import com.lps.appinfo.model.*;
import com.lps.appinfo.service.ApplicationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.lps.appinfo.service.DatabaseInfoService;
import com.lps.appinfo.service.EnvironmentService;
import com.lps.appinfo.service.ServerService;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
@Controller
public class ApplicationInformationController {
private static final Logger logger = LoggerFactory.getLogger(ApplicationInformationController.class);
@Autowired
private ApplicationService applicationService;
@Autowired
private EnvironmentService environmentService;
@Autowired
private ServerService serverService;
@Autowired
private DatabaseInfoService databaseInfoService;
@RequestMapping(value = "/findApplications", method = RequestMethod.GET, headers = "Accept=application/json")
public
@ResponseBody()
List<Application> findApplications() {
/* MappingJackson2HttpMessageConverter json = new MappingJackson2HttpMessageConverter();
MediaType jsonMimeType = MediaType.APPLICATION_JSON;*/
applicationService = new ApplicationService();
List<Application> applications = applicationService.getAll();
// json.write(new Object(), jsonMimeType, new ServletServerHttpResponse());
return applications;
}
}
当我通过Chrome Debugger调试javascript时,dataSource变量返回的是一个0的数组大小。所以我不确定发生了什么(或者没有发生)。
答案 0 :(得分:2)
有两个问题:
transport
定义中,有一个名为read
的成员,但url
提供为url
而不是read
。 所以,它应该是:
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url : "/appinfo/findApplications",
dataType: "json"
}
},
pageSize : 10
});