JTable jQuery integrate with Spring MVC 3 issue

坚强是说给别人听的谎言 提交于 2019-12-06 06:57:52

Add Jackson library, maven dependency in your 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>

Now you can add json property annotation to your fields in the class so that the json output is rendered as you expect. You may write a generic class (JsonJtableResponse in your question) as given below:

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
}

Now, your controller may say

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

Hope this helps.

JSON is case-sensitive. So it would not be able to match "result" with "Result". I had faced the same issue with my app as well.

So please ensure that your JSON response returns Result and Records in the correct case.

The problem was that JSON response which was required for JTable to be case sensitive.

NOT

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

but

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

From the type of error you are facing you are missing some js file to be imported .

have you added the json2.js which is an external dependency for jtable

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

it is mentioned in the following example for jtable and it is missing in your code

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

also check with the help of firebug that all the js file are loaded or not.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!