2013-01-14 23 views
2

我有JTable整合的问题了Spring MVC的JTable jQuery的使用Spring MVC 3问题整合

** * *** JSP代码 ** * ** * ** * **

<link href="http://www.jtable.org/Scripts/jtable/themes/metro/blue/jtable.css" rel="stylesheet" type="text/css" /> 
<link href="http://www.jtable.org/Content/themes/metroblue/jquery-ui.css" rel="stylesheet" type="text/css" /> 

<script src="http://www.jtable.org/Scripts/jquery-1.8.3.min.js" type="text/javascript"></script> 
<script src="http://www.jtable.org/Scripts/jquery-ui-1.9.2.min.js" type="text/javascript"></script> 
<script src="http://www.jtable.org/Scripts/jtable/jquery.jtable.js" type="text/javascript"></script> 

<script> 
$(document).ready(function() { 

//setup the jtable that will display the results 
$('#PeopleTableContainer').jtable({ 
     title: 'Table of people', 

     actions: { 
      listAction: '/admin/getalltherole', 
      createAction: '/admin/addRole', 
      updateAction: '/admin/updateRole', 
      deleteAction: '/admin/deleteRole' 
     }, 
     fields: { 
     custId: { 
       key: true, 
       list: false 
      }, 
      name: { 
       title: 'Full Name', 
       width: '30%' 
      }, 
      birthYear: { 
       title: 'Birth Year', 
       width: '15%' 
      }, 
      employer: { 
       title: 'Employer', 
       width: '25%' 
      }, 
      infoAsOfDate: { 
       title: 'As Of Date', 
       width: '15%' 
      }, 
      disabled: { 
       title: 'Status', 
       width: '15%' 
      } 
     } 
    }); 
    $('#PeopleTableContainer').jtable('load'); 
}); 

</script> 

<div id="PeopleTableContainer" style="width: 600px;"></div> 

** * ** 春季控制器 ** * ** * ** * ***

@RequestMapping(value = "/admin/getalltherole", method = RequestMethod.POST) 
@ResponseBody 
public JsonJtableResponse getalltherole(){ 
    JsonJtableResponse jstr = new JsonJtableResponse(); 
    jstr.setResult("OK"); 
    List<Role> roleList = testService.getAllRoles(); 
    jstr.setRecords(roleList); 
    return jstr; 
} 

@RequestMapping(value = "/admin/addRole", method = RequestMethod.POST) 
@ResponseBody 
public JsonJtableResponse insert(@ModelAttribute Role role, BindingResult result) { 
    JsonJtableResponse jsonJtableResponse = new JsonJtableResponse(); 
    if (result.hasErrors()) { 
     jsonJtableResponse.setResult("ERROR"); 
    } 
    try { 
     Role newRole = testService.saveRole(role); 
     //jsonJtableResponse.setRole(newRole); 
    } catch (Exception e) { 
     jsonJtableResponse.setResult(e.getMessage()); 
    } 
    return jsonJtableResponse; 
} 

@RequestMapping(value = "/admin/updateRole", method = RequestMethod.POST) 
@ResponseBody 
public JsonJtableResponse update(@ModelAttribute Role role, BindingResult result) { 
    JsonJtableResponse jsonJtableResponse = new JsonJtableResponse(); 
    if (result.hasErrors()) { 
     jsonJtableResponse.setResult("Error"); 
     return jsonJtableResponse; 
    } 
    try { 
     testService.updateRole(role); 
     jsonJtableResponse.setResult("OK"); 
    } catch (Exception e) { 
     jsonJtableResponse.setResult(e.getMessage());   
    } 
    return jsonJtableResponse; 
} 

@RequestMapping(value = "/admin/deleteRole", method = RequestMethod.POST) 
@ResponseBody 
public JsonJtableResponse delete(@RequestParam Integer custId) { 
    JsonJtableResponse jsonJtableResponse = new JsonJtableResponse(); 
    try { 
     testService.deleteRole(custId); 
     jsonJtableResponse.setResult("OK"); 
    } catch (Exception e) { 
     jsonJtableResponse.setResult(e.getMessage());    
    } 
    return jsonJtableResponse; 
} 


JSON response object 

public class JsonJtableResponse { 

    private String Result; 

    private List<Role> Records; 

    public String getResult() { 
     return Result; 
    } 

    public void setResult(String Result) { 
     this.Result = Result; 
    } 

    public List<Role> getRecords() { 
     return Records; 
    } 

    public void setRecords(List<Role> Records) { 
     this.Records = Records; 
    } 
} 

** * ** *获得JSON响应* ** * ** * **

{ 
    "result":"OK", 
    "records":[ 
     { 
     "custId":"1", 
     "name":"aaa", 
     "birthYear":"1982", 
     "employer":"xxx", 
     "infoAsOfDate":"20130110", 
     "disabled":"true" 
     }, 
     { 
     "custId":"2", 
     "name":"bbb", 
     "birthYear":"1982", 
     "employer":"xxx", 
     "infoAsOfDate":"20130111", 
     "disabled":"true" 
     }, 
     { 
     "custId":"3", 
     "name":"ccc", 
     "birthYear":"1982", 
     "employer":"xxx", 
     "infoAsOfDate":"20130108", 
     "disabled":"false" 
     }, 
     { 
     "custId":"4", 
     "name":"ddd", 
     "birthYear":"1981", 
     "employer":"xxx", 
     "infoAsOfDate":"20130107", 
     "disabled":"true" 
     } 
    ] 
} 

ISSUE * ** * ** * ** * ** * ****

我可以使用萤火虫控制台获得给定的JSON响应。

但是当页面加载它抛出萤火控制台上的错误,即使JSON数据正确地被加载,显示在数据表

"NO Data available" 

消息。

并且控制台上也有错误。

"TypeError: this._$errorDialogDiv.html(message).dialog is not a function" 

正如我已经搜查,这个错误通常是由于jquery UI库没有正确添加。

当我改变 - 的listAction:'/admin/getalltherole'一些不存在的URL

"An error occured while communicating to the server." is displayed in a dialog box. 

的jQuery-UI-1.9.2.min.js包括所有必要的jQuery UI的库,我试图将所有的库单独也是如此,没有任何运气。

任何含义?

回答

2

添加杰克逊库,Maven的依赖在你的pom.xml:

<properties> 
    <jackson.version>1.9.10</jackson.version> 
</properties> 
<dependency> 
    <groupId>org.codehaus.jackson</groupId> 
    <artifactId>jackson-mapper-asl</artifactId> 
    <version>${jackson.version}</version> 
</dependency> 

现在你可以JSON属性标注的类,以便为您所期望的JSON输出呈现添加到您的领域。

public class JTableJSONResponse<T> { 
    @JsonProperty("Result") 
    private String result; 

    @JsonProperty("Records") 
    private List<T> records; 

    @JsonProperty("Message") 
    private String message; 

    @JsonProperty("TotalRecordCount") 
    private int totalRecordCount; 

    public JTableJSONResponse(String result, List<T> records, int totalRecordCount) { 
     super(); 
     this.result = result; 
     this.records = records; 
     this.totalRecordCount = totalRecordCount; 
    } 
    //getters and setters 
} 

现在,您的控制器可说

List<Role> roleList = roleService.getAllRoles(); 
return new JTableJSONResponse<Role>("OK",roleList,roleList.size()); 

希望这有助于:下面给出您可以编写一个通用类(JsonJtableResponse在你的问题)。

1

从您遇到的错误类型您缺少一些要导入的js文件。

你添加的json2.js这是JTable的

https://github.com/hikalkan/jtable/tree/master/lib/external

它在下面的例子中为JTable中提到的,它是在你的代码所缺少的外部依赖

http://www.jtable.org/Tutorials/UsingWithAspNetWebFormsPageMethods#CreatePage

也检查与萤火虫的帮助下,所有的js文件都加载与否。

+0

嗨Meherzad, 所有js文件都正确加载。我也添加了json2.js,但没有运气,但仍然是相同的错误。 – Sanath

1

问题是JTable需要的JSON响应是区分大小写的。

{ 
"result":"OK", 
"records":[ 
    ....... 
    ]} 

{ 
    "Result":"OK", 
    "Records":[ 
    ....... 
    ]} 
+1

https://github.com/hikalkan/jtable/issues/70 – Sanath

+0

嗨,我已经做了适当的大写在我的代码为JSON。但我面临同样的问题。 “与服务器通信时发生错误” 当第一个选项卡未完全加载时,此警报显示在我的应用程序中,当我从一个选项卡重定向到另一个选项卡时。 你有什么想法我为什么要面对这个问题,我该如何解决这个问题? –

3

JSON是区分大小写的。所以它不能匹配“结果”和“结果”。我也遇到了与我的应用程序相同的问题。

所以请确保您的JSON响应以正确的方式返回Result和Records。