在学习EasyUI过程中遇到的问题(乱码问题,分页问题)
程序员文章站
2022-04-07 15:37:36
...
乱码问题
在学习datagrid表格时,从后台获取数据.
要从数据库中获取数据后,要放入JSON中.
JSON的格式如下:
{"total":12,
"rows":[
{"id":1,"email":"aaa@qq.com","age":18,"name":"小明","dept":1},
{"id":2,"email":"aaa@qq.com","age":25,"name":"小李","dept":2},
{"id":3,"email":"aaa@qq.com","age":21,"name":"夏红","dept":3},
{"id":4,"email":"aaa@qq.com","age":45,"name":"王宏","dept":1},
{"id":5,"email":"aaa@qq.com","age":22,"name":"杜拉拉","dept":2},
{"id":6,"email":"aaa@qq.com","age":35,"name":"王伟","dept":3},
{"id":7,"email":"aaa@qq.com","age":24,"name":"李斯特","dept":1},
{"id":8,"email":"aaa@qq.com","age":26,"name":"刘备","dept":2},
{"id":9,"email":"aaa@qq.com","age":41,"name":"王明","dept":3},
{"id":10,"email":"aaa@qq.com","age":42,"name":"李磊","dept":1},
{"id":11,"email":"aaa@qq.com","age":38,"name":"杜明","dept":2},
{"id":12,"email":"aaa@qq.com","age":29,"name":"赵光义","dept":3}]
}
其中total为总记录数(注:是所有数据的记录数,这个属性会影响到分页功能,而不是当前获取到的list的数量)
rows为具体的内容,这是一个json数组,数组中的每条json都是一条记录.
json.toString()返回到前台.
两种方式,一种就是通过注解@@ResponseBody,用return直接返回就行
另一种就是 方法中加一个参数PrintWriter out,然后就 out.write(json.toString());
前台也可以接收到
另外,如果json中有中文,前台会产生乱码.
这时我们就要设置一个属性,就是上图绿色圈子圈出来的produces={"application/json;charset=utf-8"}
这样就OK了
页码问题
在JS文件中,设置pagination属性,就可以开启分页插件
这里要提出的是,这里的分页插件,也仅仅是前台的分页插件,其中的各种按钮也都是往后台传参数,具体的分页后的数据也是需要根据参数来从数据库中获取的
通过浏览器的开发者工具可查看到,每次点击按钮,都会向后台传两个参数,page和rows
page为当前页数,rows为每页显示的记录数.
后台是可以接收到这两个参数的,如下图
在service层就可以利用这两个参数,然后再配合 PageHelper 插件 就可以轻松的进行分页操作了.
上一篇: 用C语言编写排雷游戏(多文件)